简体   繁体   中英

Rails Routing Issue

This is a really basic issue in rails that I couldn't find a clear explanation in tutorials. If you make a controller, then add a view to it and route. how would you allow another model to access by passing in it's id. I know that's not really clear, below is what I mean.

I have a model named user

I have a controller named search with the view and function find (in this function I do @user = User.find(params[:id]) and I use @user in the view find) I also have get "search/find" in routes

if I do rake routes i now have the route search_find.

in the users show if I do link_to 'Search in area', search_find_path(@user), it tells me couldn't find user without and id

if i do match '/users/:id/search/find', :to => 'search#find', then link_to 'Search in area', @user.id.to_s() + '/search/find' in the show I can make it work, but I need a cleaner route because that route doesn't seem reliable to work in any page

Try to change

 get "search/find"

to

 get "search/find/:id"

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