问题
I'm creating a custom view that is a slight modification of the index.html.erb
. I'd like to be able to create a link on my web app that directs a user to this custom view called list.html.erb
.
Here's what I've done:
1) Copied the default scaffold index view and renamed it to list.html.erb
2) Modified GalleriesController
by copying the index
method and renaming to list
:
def list
@galleries = Gallery.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @galleries }
end
end
3) Modified routes.rb
file like so:
match "galleries/list" => "galleries#list"
I keep getting the following error:
Couldn't find Gallery with ID=list
Rails.root: /Users/scervera/Sites/MDN
Application Trace | Framework Trace | Full Trace
app/controllers/galleries_controller.rb:28:in `show'
In my search on stackoverflow I was unable to find any similar questions.
回答1:
I'm guessing you put the match
outside of, and after, the gallery resources
routing.
This means the list
is being interpreted as the :id
of the default RESTful mapping.
Options include:
- Just using
index
unless you truly need them both (which seems weird). - Adding a
list
RESTful action as described here (see below). - Changing the order of your routing and/or using a constraint to avoid route overlap. IMO this is the most-fragile and least-preferable.
To add the list
action (option 2):
resources :galleries do
get 'list', :on => :collection
end
回答2:
You should put your galleries/list
route before all other gallery routes.
Order matters. In your case, route "galleries/:id" gets matched first and causes this error.
You can get exhaustive information about Rails routing here: Rails Routing from the Outside In.
来源:https://stackoverflow.com/questions/8694329/creating-custom-view-similar-to-index-html-erb