0

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

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

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

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"

所以不确定我的路线是否正确。我想知道资源 :mailbox 是否正确,因为我没有大量资源,它是 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
4

1 回答 1

0

您可能希望将邮箱定义为路由中的单一资源。否则,Rails 将希望您同时传递用户 id 和邮箱 idmember_mailbox_path以路由到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

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

要渲染mailboxes#show,您需要添加一个MailboxesController带有显示路线的路线,这可能会查找成员:

class MailboxesController < ApplicationController

  def show
    @member = Member.find(params[:member_id])
    # other mailbox code...
  end

end 

您还将创建一个模板app/views/mailboxes/show.html.erb来呈现邮箱显示页面。

另外,我建议不要深度嵌套您的路线,如第三级:收据。

于 2012-10-09T17:26:45.967 回答