创建类似于 index.html.erb 的自定义视图 [英] Creating custom view similar to index.html.erb

查看:32
本文介绍了创建类似于 index.html.erb 的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个对 index.html.erb 稍作修改的自定义视图.我希望能够在我的网络应用程序上创建一个链接,将用户定向到这个名为 list.html.erb 的自定义视图.

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.

这是我所做的:

1) 复制默认脚手架索引视图并重命名为list.html.erb
2)通过复制index方法并重命名为list来修改GalleriesController:

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) 修改 routes.rb 文件如下:

3) Modified routes.rb file like so:

match "galleries/list" => "galleries#list"

我不断收到以下错误:

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'

在我对 stackoverflow 的搜索中,我找不到任何类似的问题.

In my search on stackoverflow I was unable to find any similar questions.

推荐答案

我猜你把 match 放在画廊 resources 路由之外和之后.

I'm guessing you put the match outside of, and after, the gallery resources routing.

这意味着 list 被解释为默认 RESTful 映射的 :id.

This means the list is being interpreted as the :id of the default RESTful mapping.

选项包括:

  1. 只使用 index 除非你真的需要它们(这看起来很奇怪).
  2. 添加 list RESTful 操作,如这里(见下文).
  3. 更改路线顺序和/或使用约束来避免路线重叠.IMO 这是最脆弱和最不受欢迎的.
  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.

要添加 list 操作(选项 2):

To add the list action (option 2):

resources :galleries do
  get 'list', :on => :collection
end

这篇关于创建类似于 index.html.erb 的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆