简体   繁体   中英

user_path Not working in association

Ruby on rails application have two model User and Location

User model

belongs_to :location

Location model

has_many :users

routes.rb

devise_for :users
resources :users

This view in location show page

<% @location.users.each do |locuser| %>
   <%= link_to locuser.email, user_path %><br>      
<% end %>

Error is Couldn't find User with 'id'

This my Users Controller

def show
 @user = User.find(params[:id])
end

I added <%= link_to locuser.email, current_user %> it also not working.

I am considering you are not using devise routes. where no need to pass user object to routes. In your show action you do need id so I am passing user object for show path. If this is not the case run rake routes | grep user rake routes | grep user and paste log here

it should be user_path(locuser)

try change :

<% @location.users.each do |locuser| %>
   <%= link_to locuser.email, user_path(locuser) %><br>      
<% end %>

or

<% @location.users.each do |locuser| %>
   <%= link_to locuser.email, locuser %><br>      
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM