Rails路由嵌套视图

时间:2014-02-07 16:59:09

标签: ruby-on-rails

我有一个来自嵌套控制器的管理员视图,列出了来自邮箱宝石的所有会话,请参见:Mailboxer Gem, Admin View)。

我无法正确路由。在管理视图中创建的链接转到/ conversation / id,我需要它转到admin / conversations / id。输入浏览器栏时两条路线都有效,并显示正确的视图。

在我的控制器中:

 # GET /admin/conversations
  # GET /admin/conversations.json
  def index
    @admin_conversations = Conversation.all.unscoped.order("updated_at desc")
  end

# GET /admin/conversations/1
# GET /admin/conversations/1.json
def show
  @conversation = Conversation.find(params[:id])
end

在我看来:

<% @admin_conversations.each do |admin_conversation| %>
  <tr>
    <td><%= admin_conversation.originator.name %> <span class="label label-default"><%= admin_conversation.originator.role.humanize %></span> </td>
    <td><%= admin_conversation.subject %> </td>
    <td><%= time_ago_in_words(admin_conversation.updated_at) %> ago</td>
    <td><%= link_to 'View Conversation', admin_conversation %></td>

  </tr>

视图中创建的链接转到/ conversation / id,我需要它转到admin / conversations / id。

我的管理路线:

namespace :admin do
    resources :users
    resources :managers
    resources :conversations
    get 'enquiries/:status', to: 'enquiries#index', as: :admin_enquiries_with_status, constraints: {status: /(pending|completed|approved)/}
    resources :enquiries, except: [:destroy] do
      post :add_venue, on: :member
      post :remove_venues, on: :member
      post :approve, on: :member
    end
    resources :venues, only: [:index, :show, :destroy] do
      get :autocomplete, on: :collection
    end
    get "pages/statistics"
  end

我尝试使用rails docs来处理名称空间路由,但似乎无法使其正常工作。如果我在浏览器栏中输入实际路径/ admin / conversations / id,则会从​​正确的控制器返回显示页面。我只是不确定如何正确显示链接?

对话控制器路线:

 send_message POST   /conversations/send_message(.:format)    conversations#send_message
           reply_conversation POST   /conversations/:id/reply(.:format)  conversations#reply
           trash_conversation POST   /conversations/:id/trash(.:format)  conversations#trash
         untrash_conversation POST   /conversations/:id/untrash(.:format)  conversations#untrash
                conversations GET    /conversations(.:format) conversations#index
                              POST   /conversations(.:format) conversations#create
             new_conversation GET    /conversations/new(.:format) conversations#new
                 conversation GET    /conversations/:id(.:format) conversations#show

管理员对话控制器路线:

POST   /admin/conversations(.:format) admin/conversations#create
       new_admin_conversation GET    /admin/conversations/new(.:format) admin/conversations#new
      edit_admin_conversation GET    /admin/conversations/:id/edit(.:format)    admin/conversations#edit
           admin_conversation GET    /admin/conversations/:id(.:format) admin/conversations#show
                              PATCH  /admin/conversations/:id(.:format)  admin/conversations#update
                              PUT    /admin/conversations/:id(.:format)  admin/conversations#update
                              DELETE /admin/conversations/:id(.:format)  admin/conversations#destroy

1 个答案:

答案 0 :(得分:0)

好的,好好解决了。

<td><%= link_to 'View Conversation', [:admin, admin_conversation] %></td>

只需要删除:admin并在其中放置方括号以将/ admin添加到网址。

应该稍微阅读一下文档......