ruby-on-railscustom-routes

Routing Error - Custom Contoller Method (Adding Custom routes in resource routes)


I want to add link to friends list but i get routing error

No route matches {:action=>"friend_list", :controller=>"users_controller"}

users_controller

def friend_list
    @frnds =  User.find_friends(current_user)
end

routes

devise_for :users
resources :users do
member do
  get :follow
  get :unfollow
  get :show
end
match 'users/:id/friend_list' => 'users#friend_list', via: [:get]

link

<li><%= link_to "Friends",  :controller => :users_controller, :action => :friend_list%></li>

end match 'users/:id/friend_list' => 'users#friend_list', via: [:get] root 'home#front'


Solution

  • instead of member block use collection block like this

    resources :users do
      collection do
        get "follow"
        get "unfollow"
        get "show"
     end
    end
    

    member block will append :id in routing while collection block will allow you add custom routes in resource routes

    in link_to use this

    <%=link_to "Friends", controller: "users", action: "friend_list"%>