Creating custom view similar to index.html.erb

北战南征 提交于 2019-12-11 06:19:33

问题


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:

  1. Just using index unless you truly need them both (which seems weird).
  2. Adding a list RESTful action as described here (see below).
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!