Adam Parker
Adam Parker

Reputation: 107

Rails routes error with namespace?

So I have created two routes using a 'Member' namespace, when I do rake routes, it does not display the path that I should use it is just showing this;-

GET /user/:id(.:format) member/member#

GET /user/:id/edit(.:format) member/member#edit

when I use this line it returns an error;-

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

and get this error;-

No route matches {:action=>"show", :controller=>"Member/Member"}

This is my routes;-

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

My whole rake routes;-

                  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

How would I access my show action and my edit action?

Upvotes: 1

Views: 642

Answers (3)

tokhi
tokhi

Reputation: 21626

I am doing something like below for versioning in rails 4:

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

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

routes:

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

I have the controllers in the app/v1 and app/v2 directories

Upvotes: 0

MrYoshiji
MrYoshiji

Reputation: 54902

I suggest you to use the Rails built-in resources in your routes:

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

And then you will be able to call the following paths:

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

Instead of using the old-fashioned link definition:

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

Upvotes: 2

Pavan
Pavan

Reputation: 33552

No route matches {:action=>"show", :controller=>"Member/Member"}

The problem is not with the routes, but with the link itself. You need to change it to below

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

Notice the change :controller => "Member/Member" to :controller => "member/member"

Upvotes: 1

Related Questions