6

I am receiving a no route matches error from the line <%= link_to "Ask User Out", askout_user_message_path(@user), :class => "button" %>.

This used to work before I added a gem but now it stopped working. I tried moving under collection but I get no luck with that as that's where it used to be.

Routes:

 resources :users do |user|

 resources :messages do
   member do
     post :new
     get 'askout', action: 'askout'
   end
 end
  collection do
     get :trashbin
     post :empty_trash

end
 end

 resources :conversations do
   member do
     post :reply
     post :trash
     post :untrash
   end
 end

Old Routes:

 resources :users do |user|

    resources :messages do
      collection do
        post 'delete_multiple'
        get 'askout', action: 'askout'
        get 'reply', action: 'reply'
      end
    end
  end

My routes changed as I added mailboxer gem.

pwz2000
  • 1,385
  • 2
  • 16
  • 50
  • This nested route is expection a message id since you defined askout in the member of the resource :messages, use the helper like this : `askout_user_message_path(@user, message)` where message is the Message found with params[:id] --- If you don't want it to need a message id, put it in the collection block and use `askout_user_messages_path(@user)` (plural on messages) – MrYoshiji Feb 28 '14 at 19:44

1 Answers1

6

You'd be better doing this:

   #config/routes.rb
   resources :users do
     resources :messages do
       member do
         post :new
         get :askout
       end
     end
     collection do
         get :trashbin
         post :empty_trash
      end
   end

This will give you:

users/1/messages/5/askout

What I think you want:

   #config/routes.rb
   resources :users do
     resources :messages do
       post :new
       collection do
         get :askout
       end
     end
     collection do
         get :trashbin
         post :empty_trash
      end
   end

This will give you:

users/2/messages/askout

The path helper will be as determined in the rake routes view -- you should look at it to get an idea as to what your route is called (allowing you to write it accordingly)

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • i was having in understanding between collection and member. It's like if i want `courses/:id/features` then it should be under member, if just `courses/features` then collection. Am i understand it right ? – 7urkm3n Mar 30 '16 at 18:34
  • collection = `courses/features`, member = `courses/:id/features`, "none" = `courses/:course_id/features` – Richard Peck Mar 30 '16 at 18:43
  • 1
    In member = `courses/:id/features`, what kind `ID` has tobe passed. if its nested routes like in your answer. – 7urkm3n Mar 30 '16 at 18:45
  • 1
    Nested resources will automatically create a series of `member` / `collection` routes for the controller you're referencing. EG `resources :x do resources :y end` will result in `collection x/:x_id/y`. If you're manually setting a `member` route for your nested resource, you'll have to set the *`id`* for your nested record, and `:parent_id` for the parent. If you write a question, I'll write a detailed answer for you if you need – Richard Peck Mar 30 '16 at 18:47
  • No problem! Offer still stands for the question :D – Richard Peck Mar 30 '16 at 19:55
  • there is link about Rails routes. http://stackoverflow.com/questions/36384313/rails-routes-nested-member-collection-namespace-scope-and-customizable – 7urkm3n Apr 03 '16 at 10:37