How to correctly set up routes when creating a new view in Ruby on Rails?
I created a new action called "asked". asked.h开发者_Go百科aml is in views/questions as it should be. I've also added
def asked
respond_to do |format|
format.html
format.xml { render :xml => @questions }
end
end
to the Questions controller for this action.
My problem is that when I got to the url http://localhost:3000/questions/asked, I get this error:
ActiveRecord::RecordNotFound in QuestionsController#show
Couldn't find Question with ID=asked
So, I Googled this and found out that I needed to change the way I route things.
I tried: map.connect ':controller/asked', :action => 'asked'
and map.resources :questions, :collection => {:asked => :get}
, but to no avail.
Obviously I don't fully understand how Rails mapping works, but if someone would let me know what's going on, I'd really appreciate it!
Rails routing works by picking the first route that matches the requested URL.
From your description it seems that you put your map.connect
statements to the bottom of your routes.rb
. You should place it before map.resources :questions
, since otherwise the URL /questions/asked
is matched by map.resources
as a show
action.
精彩评论