LilasAll
LilasAll

Reputation: 47

How to find the correct path for a link in rails

I got a problem with a link I've tried to make. I want to have a page with an article, and a link to the author page of it. Iv's try 100 differents version but i don't know how to do... Here is my code :

<a <% link_to " Par : #{@article.author.pseudo}", user_path(User.where(user_id: @author_id) %></a>

or with this path : user_path(Article.find(params[:id]).author_id)

If you know how I can resolve that... Thanks !

Upvotes: 2

Views: 137

Answers (3)

Mykhailo Melnyk
Mykhailo Melnyk

Reputation: 302

Try something like this:

<%= link_to("Par : #{@article.author.pseudo}", @article.author) %>

Upvotes: 0

Vasilisa
Vasilisa

Reputation: 4640

Don't wrap link_to helper into <a>tag, it is already generated by the helper. where returns the collection, not the instance - that's why your code does not work. You already have needed user instance in @article.author, just use it

<%= link_to "Par : #{@article.author.pseudo}", user_path(@article.author) %>

Upvotes: 2

ramblex
ramblex

Reputation: 3057

You should be able to link directly to the author:

<%= link_to "Par : #{@article.author.pseudo}", @article.author %>

Upvotes: 1

Related Questions