Error in link_to

21 views
Skip to first unread message

Bruno Oliveira

unread,
Feb 19, 2015, 5:25:43 AM2/19/15
to rubyonra...@googlegroups.com, br...@awdd.com.br

I'm with a problem using link_to.
This is my code:


<h1>Listing articles</h1>
 <%= link_to 'My Blog', controller: 'articles' %>
 <%= link_to 'New article', new_article_path %>
<table>
<tr>
   <th>Title</th>
   <th>Text</th>
  </tr>
  <% @articles.each do |article| %>
  <tr>
  <td><%= link_to article.title, articles_path(article)  %></td>
  <td><%= article.text %></td>
  <td><%= article.id%></td>
  </tr>
  <% end %>
</table>

so this back links as " http://localhost:3000/articles.2 " instead of "http://localhost:3000/articles/2" .
What should I do? I am new to ruby on rais

Frederick Cheung

unread,
Feb 19, 2015, 5:35:37 AM2/19/15
to rubyonra...@googlegroups.com, br...@awdd.com.br


On Thursday, February 19, 2015 at 10:25:43 AM UTC, Bruno Oliveira wrote:

   <td><%= link_to article.title, articles_path(article)  %></td>


That should be article_path(article) (note the singular). articles_path returns the path for the collection as a whole. It's not expecting an article so ends up interpreting as the format it should link to, which is why it links to articles.2

Fred 

Bruno Oliveira

unread,
Feb 23, 2015, 9:27:27 AM2/23/15
to rubyonra...@googlegroups.com, br...@awdd.com.br
My God! it works!

Thank you Fred, you're my new superhero.
Reply all
Reply to author
Forward
0 new messages