是一个作品集。
每个作品可能有多个图片,我就做成 photo gallery 这样的形式. 那我在创建脚手架的时候怎么弄这个图片的字段?
rails generate scaffold Work title:string description:text imgae: #这里是什么?
谢谢,另外我看教程它的 image 只是 string 字段,需要手动输入图片地址,有没有直接 upload 这样的?
我觉得可以考虑找一个 gem 把图片直接上传到 amazon S3 这样的服务方 数据库里存图片的地址就好了 image 这里用 string 就好了 数据库最好不要直接存图片进去
如果是把图片存到本地的话 image 这里储存图片的本地路径就行了
这个 carriewave 哪里出错了? bundl show:
...
* carrierwave (0.6.2)
...
Gemfile:
...
gem 'carrierwave'
...
work.rb
class Work < ActiveRecord::Base
attr_accessible :description, :title, :image
mount_uploader :image, ImageUploader
end
works/_form.html.erb
<%= form_for(@work, :html => {:multipart => true}) do |f| %>
# rest field
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
# simbit
<% end %>
打开http://0.0.0.0:3000/works,:返回
NoMethodError in WorksController#index
undefined method `key?' for nil:NilClass
这是我完全参照视频教程来做的,不知道哪里出错了
http://0.0.0.0:3000/works/new:也是返回
NoMethodError in WorksController#new
undefined method `key?' for nil:NilClass
是我在 works controller 里面少什么?但是教程里面没有提到
我看到教程是只有加了这些它那里就可以上传了,我这儿报错。
class Work < ActiveRecord::Base
attr_accessible :description, :title, :image
mount_uploader :image, ImageUploader
end
是不是这里?
报错:
NoMethodError in WorksController#new
undefined method `key?' for nil:NilClass
Rails.root: /Users/yuwen/Documents/bitbucket/ctd
Application Trace | Framework Trace | Full Trace
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
db/migrate/20120725115122_add_image_to_works.rb
class AddImageToWorks < ActiveRecord::Migration
def change
add_column :works, :image, :string
end
end
db/migrate/20120725114714_create_works.rb
class CreateWorks < ActiveRecord::Migration
def change
create_table :works do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# gem 'jbuilder'
# gem 'debugger'
gem 'carrierwave'
# gem 'paperclip',:git => 'git://github.com/thoughtbot/paperclip.git'
shell 里面:
➜ ctd git:(master) ✗ rails g uploader image
create app/uploaders/image_uploader.rb
➜ ctd git:(master) ✗ rails g scaffold Work title:string description:text
invoke active_record
create db/migrate/20120725114714_create_works.rb
create app/models/work.rb
invoke test_unit
create test/unit/work_test.rb
create test/fixtures/works.yml
invoke resource_route
route resources :works
invoke scaffold_controller
create app/controllers/works_controller.rb
invoke erb
create app/views/works
create app/views/works/index.html.erb
create app/views/works/edit.html.erb
create app/views/works/show.html.erb
create app/views/works/new.html.erb
create app/views/works/_form.html.erb
invoke test_unit
create test/functional/works_controller_test.rb
invoke helper
create app/helpers/works_helper.rb
invoke test_unit
create test/unit/helpers/works_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/works.js.coffee
invoke scss
create app/assets/stylesheets/works.css.scss
invoke scss
conflict app/assets/stylesheets/scaffolds.css.scss
Overwrite /Users/yuwen/Documents/bitbucket/ctd/app/assets/stylesheets/scaffolds.css.scss? (enter "h" for help) [Ynaqdh] n
skip app/assets/stylesheets/scaffolds.css.scss
➜ ctd git:(master) ✗ rake db:migrate
== CreateWorks: migrating ====================================================
-- create_table(:works)
-> 0.0015s
== CreateWorks: migrated (0.0016s) ===========================================
➜ ctd git:(master) ✗ rails g migration add_image_to_works image:string
invoke active_record
create db/migrate/20120725115122_add_image_to_works.rb
➜ ctd git:(master) ✗ rake db:migrate
== AddImageToWorks: migrating ================================================
-- add_column(:works, :image, :string)
-> 0.0399s
== AddImageToWorks: migrated (0.0400s) =======================================
#10 楼 @Rei 是在 work_controller 里面?
class WorksController < ApplicationController
# GET /works
# GET /works.json
def index
@works = Work.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @works }
end
end
# GET /works/1
# GET /works/1.json
def show
@work = Work.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @work }
end
end
# GET /works/new
# GET /works/new.json
def new
@work = Work.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @work }
end
end
# GET /works/1/edit
def edit
@work = Work.find(params[:id])
end
# POST /works
# POST /works.json
def create
@work = Work.new(params[:work])
respond_to do |format|
if @work.save
format.html { redirect_to @work, notice: 'Work was successfully created.' }
format.json { render json: @work, status: :created, location: @work }
else
format.html { render action: "new" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# PUT /works/1
# PUT /works/1.json
def update
@work = Work.find(params[:id])
respond_to do |format|
if @work.update_attributes(params[:work])
format.html { redirect_to @work, notice: 'Work was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# DELETE /works/1
# DELETE /works/1.json
def destroy
@work = Work.find(params[:id])
@work.destroy
respond_to do |format|
format.html { redirect_to works_url }
format.json { head :no_content }
end
end
end
actionpack (3.2.6) lib/action_controller/metal/hide_actions.rb:36:in `visible_action?'
actionpack (3.2.6) lib/action_controller/metal/hide_actions.rb:18:in `method_for_action'
actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:14:in `method_for_action'
actionpack (3.2.6) lib/action_controller/metal/compatibility.rb:61:in `method_for_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:115:in `process'
actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__1603190885298670096__call__4510840069410552607__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
我看不出了。你逐步注释掉一些代码看看是哪里导致出问题。我搜了一下 undefined method `key?' for nil:NilClass 有可能是拼写错误导致的。
还有问题,carrierwave 只能传一个图片 (不过弄完之后我发现这个确实很方便), 现在我想可以传重复的图片。
我刚才试了 jQuery File Upload 的 Basic plugin (minimal setup guide) 和 Rails + (Carrierwave || Dragonfly) Rails-setup-for-V6 (user contribution),但是都没有成功。
还有看到支持 rails 的 tutorial 都要 amazon s3(我没有美元信用卡申请不了),还有看到一个 google app engine 的,小试了一下,怀疑得配置更多东西,懒得弄了。
不知道上传多张图片得还有什么好得 gem,专门正对 rails 的。今天进度缓慢,只搞了这个 carrierwave!