2011-08-21 63 views
0

我做过rails g controller World并创建了一个新的布局,也称为world。我现在有app/views/world/index.html.erb。这是我WorldControllerRails noob问题:使用不同的布局渲染视图

class WorldController < ApplicationController 
    before_filter :login_required 

    layout "world" 

    def show 
    #?? 
    end 

end 

我不知道要放什么东西在我的高清节目,这样我可以导航到localhost:3000/world/index,并有views/world/index页面呈现。任何帮助,将不胜感激。

回答

1

显示引用的路线中的行动,而不是“显示它”的命令。相反,您需要定义index操作。

def index 
end 

如果这不起作用..有可能是路由问题。向我们显示config/routes.rb

+0

好的,我添加了'def index @msgs = current_user.all_msgs.paginate(:per_page => 10,:页=> PARAMS [:页]) @last_msg = current_user.msgs.last 端 DEF显示 @user = User.find_by_username(PARAMS [:用户名]) @msg = @ user.all_msgs end'但现在我得到了'nil:NilClass'的undefined方法'all_msgs',但同样的def show在我的其他视图中工作? – user852974

+0

你可以显示application_controller.rb或者current_user和login_required被定义的任何地方吗? – natedavisolds

0

您的控制器以单数名称命名为“World”。这通常意味着你的“世界”资源是单一的。即只有一个世界(世界不多)。如果是这样的话,就不会有“索引”。您可以定义路线是这样的:

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定义您的路线。在后一种情况下,没有索引方法(因为有一个单一的世界,不需要索引)