路线工作一个地方,而不是其他地方

时间:2012-10-09 17:07:18

标签: ruby-on-rails routes custom-routes

这有点难以沟通,但我会尝试不粘贴我的所有代码。我有会员谁有一个邮箱有很多收据。在标题布局中,我有一个调用

的导航
<%= link_to "Message Center", member_mailbox_path(current_user.member_id) %>

它适用于大多数页面,例如trail /#,各种模型的资源页面 但在其他页面上,似乎是自定义路由页面,我收到此错误

No route matches {:action=>"show", :controller=>"mailbox", :member_id=>16}

运行rake路线显示:

member_mailbox GET    /members/:member_id/mailbox/:id(.:format)      mailbox#show

路线让我感到困惑,这是我解决这个问题的路线(显示消息尚未测试)......

resources :members do
 resources :mailbox do
  resources :receipts do
    member do
      get :show_message
    end
  end
 end
end

显示错误的页面的路由与此类似

match '/my_plays', :to => "trails#my_plays"
match '/my_creations', :to => "trails#my_creations"

所以不确定我的路线是否正确。我想知道资源:邮箱是否正确,因为我没有一堆资源,它是一个has_one .... THX

----编辑---改变路线后的建议:

member_mailbox POST   /members/:member_id/mailbox(.:format)                            mailboxes#create
new_member_mailbox GET    /members/:member_id/mailbox/new(.:format)                       mailboxes#new
edit_member_mailbox GET    /members/:member_id/mailbox/edit(.:format)                      mailboxes#edit
                    GET    /members/:member_id/mailbox(.:format)                           mailboxes#show
                    PUT    /members/:member_id/mailbox(.:format)                           mailboxes#update
                    DELETE /members/:member_id/mailbox(.:format)                           mailboxes#destroy

1 个答案:

答案 0 :(得分:0)

您可能希望将邮箱定义为singular resource in your routes。否则,Rails会希望您传入member_mailbox_path的用户ID和邮箱ID以路由到mailbox#show。我相信这就是你收到路由错误的原因。由于每个用户都有一个邮箱,因此无需在路由中添加额外的查找部分。因此,resources :mailbox代替resource :mailbox,而不是resources :members do resource :mailbox do resources :receipts do member do get :show_message end end end end

                 member_mailbox POST   /members/:member_id/mailbox(.:format)                           mailboxes#create
             new_member_mailbox GET    /members/:member_id/mailbox/new(.:format)                       mailboxes#new
            edit_member_mailbox GET    /members/:member_id/mailbox/edit(.:format)                      mailboxes#edit
                                GET    /members/:member_id/mailbox(.:format)                           mailboxes#show
                                PUT    /members/:member_id/mailbox(.:format)                           mailboxes#update
                                DELETE /members/:member_id/mailbox(.:format)                           mailboxes#destroy

我相信这会产生以下路线:

mailboxes#show

请注意,GET,PUT和DELETE旁边缺少路径名并不意味着它们不存在;它们只是重复POST路径,但每个都响应不同的HTTP方法。

要呈现MailboxesController,您需要添加一个带有show route的class MailboxesController < ApplicationController def show @member = Member.find(params[:member_id]) # other mailbox code... end end ,这可能会查找该成员:

app/views/mailboxes/show.html.erb

您还将在{{1}}创建模板以呈现邮箱显示页面。

此外,我建议反对deeply nesting your routes,如第三级:收据。