Rails索引操作 - 如何拥有'当前行'

时间:2013-12-27 01:18:58

标签: ruby-on-rails controller

我正在实现像facebook的消息传递系统这样的东西作为我的Rails应用程序的一部分(使用Rails 4.0和Ruby 2.0)。因此,假设UI有两个主要列,左列是对话列表,右侧列是当前对话的消息列表。

我在对话控制器中的主要操作是index。但是,我需要每个会话都可以点击,并在活动时收到突出显示。所以我的路由GET /conversations路由到索引方法。但是我如何拥有GET /conversations/:id路线? ......这通常是为了表演行动。我在设计控制器时遇到了麻烦。

更新:正如下面的@Fellow Stranger在答案中提到的那样,是的,我在routes.rb中有resources conversations,所以我有8个标准的Rails 4 URI模式:

    messages GET    /conversations(.:format)             conversations#index
             POST   /conversations(.:format)             conversations#create
 new_message GET    /conversations/new(.:format)         conversations#new
edit_message GET    /conversations/:id/edit(.:format)    conversations#edit
     message GET    /conversations/:id(.:format)         conversations#show
             PATCH  /conversations/:id(.:format)         conversations#update
             PUT    /conversations/:id(.:format)         conversations#update
             DELETE /conversations/:id(.:format)         conversations#destroy

即,我有conversations#show可用,但我应该怎么做,让它设置一个会话变量,然后重定向到index?这似乎有点荒谬。

2 个答案:

答案 0 :(得分:1)

RESTful index的用法是根据您的要求获取资源列表:

要获取左列中的对话列表,请使用conversations#index

要在右栏中获取特定会话的消息,请使用带有会话ID的messages#index

单击对话项时,项目高亮效果应由前端(css,js)完成。

答案 1 :(得分:0)

如果您有一个安静的设计,并在resources :conversations中使用routes.rb,那么您已经拥有GET /conversations/:id。 (如果没有,只需将get "/conversations/:id", to: "conversations#show"添加到routes.rb)。

我认为你的问题更多地关注视图层。

您可以使用页面请求呈现视图,然后将.current类添加到列表项,例如with the helper method current_page?

或者(对于更像Facebook的感觉)你可以通过ajax渲染特定的对话,然后在插入对话的同时用javascript更新列表项的类。