Search code examples
ruby-on-railsruby-on-rails-3rails-routingnested-resources

Nested resources segments misplacement


I'm trying to implement basic social network features to allow users to add, delete friends, accept and decline friedship requests.

my user resource looks like this:

resources :users
    resources :friends, :controller => :relations
end

which generates this route user_friend DELETE /users/:user_id/friends/:id

But the problem is when I access /users/1, the generated link to the delete_user_friend_path looks like this: http://localhost:3000/users/5/friends/1


Solution

  • You need to pass the user into the helper:

    delete_user_friend_path(@user, @friend)
    

    It seems that you were doing:

    delete_user_friend_path(@friend)
    

    Which will fill in the :user_id parameter, and assume you want the same :id parameter as the page you are currently on.