开发者

Rails noob question: rendering a view with a different layout

I did rails g controller World and created a new layout also called world. I now have app/views/world/index.html.erb. This is my WorldController:

class WorldController < ApplicationController
  before_filter :login_requi开发者_开发百科red

  layout "world"

  def show
    #??
  end

end

I do not know what to put in my def show so that I can navigate to localhost:3000/world/index and have the views/world/index page render. Any help would be appreciated.


Show refers to the action in the route and not a command to "show it". Instead you need to define the index action.

def index
end

If this doesn't work.. there is probably a routing problem. Show us config/routes.rb


Your controller is named "World" in the singular. This typically means your "World" resource is singular. I.e. there is just one world (not many worlds). If that's the case, there would be no "index". You might define the route like this:

resource :world

- which would give you the route

/world - mapped to WorldsController#show

This assumes the resource is singular, and there is only one world. So you don't need an id to #show it, as it's assumed only one exists (and can be found without an identifier).

If you do want multiple worlds, you'd define your routes with:

resources :worlds

- and you'd end up with the routes:

/worlds    - mapping to WorldsController#index
/world/:id - mapping to WorldsController#show

I guess the point is, are there multiple worlds? If there are, then define your routes with resources :worlds. If there is a single world, define your routes with resource :world. In the latter case, there is no index method (as there is a single World, without need of an index)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜