[英]Rails noob question: rendering a view with a different layout
我做了rails g controller World
并创建了一个新布局,也称为world
。 我现在有app/views/world/index.html.erb
。 这是我的WorldController
:
class WorldController < ApplicationController
before_filter :login_required
layout "world"
def show
#??
end
end
我不知道在def展示中放什么,这样我就可以导航到localhost:3000/world/index
并呈现views/world/index
页面。 任何帮助,将不胜感激。
显示是指路线中的动作,而不是“显示”的命令。 相反,您需要定义index
操作。
def index
end
如果这不起作用..可能是路由问题。 给我们看config / routes.rb
您的控制器以单数形式命名为“世界”。 这通常意味着您的“世界”资源是单一的。 即只有一个世界(没有很多世界)。 如果是这样,就不会有“索引”。 您可以这样定义路线:
resource :world
- which would give you the route
/world - mapped to WorldsController#show
假设资源是单一的,并且只有一个世界。 所以你并不需要一个id #show
它,因为它假定只有一个存在(可以在没有标识可以找到)。
如果您确实想要多个世界,则可以使用以下方法定义路线:
resources :worlds
- and you'd end up with the routes:
/worlds - mapping to WorldsController#index
/world/:id - mapping to WorldsController#show
我想关键是,有多个世界吗? 如果有,请使用resources :worlds
定义您的路线。 如果只有一个世界,请使用resource :world
定义您的路线。 在后一种情况下,没有索引方法(因为只有一个World,不需要索引)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.