Rails路由错误与命名空间?

时间:2016-03-15 15:25:51

标签: ruby-on-rails ruby devise

所以我使用'Member'命名空间创建了两个路由,当我做rake路由时,它没有显示我应该使用它的路径只是显示这个; -

  

GET /user/:id(.:format)成员/成员#

     

GET /user/:id/edit(.:format)成员/成员#edit

当我使用此行时,它会返回错误; -

<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "Member/Member", :action => :show} if current_user.picture? %></li>

并收到此错误; -

  

没有路线匹配{:action =&gt;“show”,:controller =&gt;“会员/会员”}

这是我的路线; -

  scope module: 'member' do
    get '/user/:id', to: 'member#show'
    get '/user/:id/edit', to: 'member#edit'
  end

我的整个佣金路线; -

                  Prefix Verb   URI Pattern                    Controller#Action
                 charges GET    /charges(.:format)             charges#index
                         POST   /charges(.:format)             charges#create
              new_charge GET    /charges/new(.:format)         charges#new
             edit_charge GET    /charges/:id/edit(.:format)    charges#edit
                  charge GET    /charges/:id(.:format)         charges#show
                         PATCH  /charges/:id(.:format)         charges#update
                         PUT    /charges/:id(.:format)         charges#update
                         DELETE /charges/:id(.:format)         charges#destroy
       new_admin_session GET    /admins/sign_in(.:format)      devise/sessions#new
           admin_session POST   /admins/sign_in(.:format)      devise/sessions#create
   destroy_admin_session DELETE /admins/sign_out(.:format)     devise/sessions#destroy
            admin_unlock POST   /admins/unlock(.:format)       devise/unlocks#create
        new_admin_unlock GET    /admins/unlock/new(.:format)   devise/unlocks#new
                         GET    /admins/unlock(.:format)       devise/unlocks#show
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        users/registrations#cancel
       user_registration POST   /users(.:format)               users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       users/registrations#new
  edit_user_registration GET    /users/edit(.:format)          users/registrations#edit
                         PATCH  /users(.:format)               users/registrations#update
                         PUT    /users(.:format)               users/registrations#update
                         DELETE /users(.:format)               users/registrations#destroy
                    root GET    /                              public/public#homepage
           cart_add_item POST   /cart_add_item(.:format)       cart/cart#add_item_to_cart
              empty_cart GET    /empty_cart(.:format)          cart/cart#empty_cart
            destroy_cart GET    /destroy_cart(.:format)        cart/cart#destroy
                    cart GET    /cart(.:format)                cart/cart#show
             product_new GET    /product/new(.:format)         admin/product#new
          product_create POST   /product/create(.:format)      admin/product#create
         product_destroy GET    /product/destroy(.:format)     admin/product#destroy
                         GET    /user/:id(.:format)            member/member#show
                         GET    /user/:id/edit(.:format)       member/member#edit

我如何访问我的节目动作和编辑动作?

3 个答案:

答案 0 :(得分:2)

我建议您在路线中使用内置resources的Rails:

scope module: 'member' do
  resources :users, only: [:show, :edit]
end

然后您就可以调用以下路径:

member_user_path(current_user) # /member/users/:id/ -> Show action
edit_member_user_path(current_user) # /member/users/:id/edit -> Edit action

而不是使用老式的链接定义:

{:controller => "member/member", :action => :show}

答案 1 :(得分:1)

  

没有路线匹配{:action =&gt;“show”,:controller =&gt;“会员/会员”}

问题不在于routes,而在于link本身。您需要将其更改为

<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "member/member", :action => :show} if current_user.picture? %></li>

请注意将:controller => "Member/Member"更改为:controller => "member/member"

答案 2 :(得分:0)

我正在为rails 4中的版本控制执行以下操作:

scope '/v1' do
    resources :foobar, module: 'v1'
end

scope '/v2' do
    resources :foobar, module: 'v2'
end

路线:

 /v1/foobar/:id
 /v2/foobar/:id

我在app/v1app/v2目录中有控制器