What is causing this NoMethodError in Ruby on Rails?
This is the code I have at the end a the create method in
Notes controller
def create
@note = Note.new(params[:note])
@note.user_id = current_user.id
respond_to do |format|
if @note.save
flash[:success] = 'Note was created.'
# @note.note_type is "Temp" at this point
if @note.note_type == "Temp"
p "passed first test"
redirect_to :controller => :categories, :action => :add_category, :note_id => @note.id, :category => 'temp category'
else
format.html { redirect_to(@note) }
format.xml { render :xml => @note, :status => :created, :location => @note }
end
else
format.html { render :action => "new" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end
end
Categories controller
def add_category
p "in add_category"
end
When it runs, I get the following error. Line 80, where the trace begins, is the respond_to do |format|
line:
NoMethodError in NotesController#create
undefined method `call' for nil:NilClass
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:175:in `respond'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:in `each'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:in `respond'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:107:in `respond_to'
/Users/benhartney/rails_projects/note_taker/app/controllers/notes_controller.rb:80:in `create'
I've got no idea 开发者_JS百科how to start debugging this error. The console shows the output line "passed first test" from the create action in the Notes controller, but not the "in add_category" output line from the add_category action in the Categories controller. I'm using Rails 2.3.5. Any help would be greatly appreciated. Thanks for reading.
The respond_to
method requires you to specify at least one format on the Responder
object passed into its block. In the code-path creating your exception, you just call redirect_to
, the responder MIME-type preference list is empty, and Rails ends up trying to call the call
method on the (non-existent) block for the (non-existent) first type preference.
精彩评论