user852974
user852974

Reputation: 2282

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_required

  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.

Upvotes: 0

Views: 154

Answers (2)

numbers1311407
numbers1311407

Reputation: 34072

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)

Upvotes: 0

natedavisolds
natedavisolds

Reputation: 4295

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

Upvotes: 1

Related Questions