I’m really starting to cry here 😉 Trying to make a link in a Models index view.
I have 2 simple Models: Users and Posts. There both generated with scaffolding and have working joins. A user has_many :posts and a post belongs_to :user.
What I’m trying to do in the views/post/index.html.er file is a list of the Post title and the user who it belongs to. It works well (also learning html5):
<% @posts.each do |post| %>
<p><%= link_to post.user.name, users_path %>: <b><%= post.title %></b></p>
<% end %>
Well, it works but the “users_path” is not what I want. I want to link to the specific User which the post belongs_to. I’m sorry to say that I don’t get much help from http://guides.rubyonrails.org/routing.html.
How should I do this? Do I have to specify a @user in the posts_controller index-action? I reallt appreciate long and detaild answears here.
Tnk soooo much for patience with a beginner 😉
you probably have this in your routes –
rake routes would generate the following mapping –
The above explains the url you can use and what objects need to be passed.
For linking the user of the post –
OR