0

I am using link_to with some HTML elements and this is what I have : <% link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %>

But I want it that it will link to the posts with the id that each post has, any help would be most appreciated.

Thank You

user1602683
  • 33
  • 1
  • 5
  • By the way i did <%= link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %> i just forgot the equals sign. – user1602683 Jun 21 '13 at 06:21
  • So are u providing this link with every post ... – Grey Jun 21 '13 at 06:23
  • yes that is correct. But it is only linking to the posts index page instead of the other way round. – user1602683 Jun 21 '13 at 06:26
  • what u need is id of specific post so that u can call show action using get method to see that post only ... – Grey Jun 21 '13 at 06:33
  • This is what the ruby code is generation remember the :id => "posts" is not literally the id for each post it is just the styling associated with the link. – user1602683 Jun 21 '13 at 06:33
  • Then what is it you want to achieve? –  Jun 21 '13 at 06:37
  • I did this instead its probably not the 100% correct but it functions fine : <%= link_to (posts_homepage.title.first(0)),(posts_homepage) , :class => "read-more" %> – user1602683 Jun 21 '13 at 07:26
  • 1
    Start learning with some rails tutorial, looks like you have no idea what you're doing. – Mike Szyndel Jun 21 '13 at 09:43
  • Michael Szyndel I am infact doing a tutorial and bumped into a problem, thats why I ASKED on stackoverflow in the first place. And yes I am a BEGINNER and would of course try diffrent things out (relatively normal!!!) and please if you want answer then dont bother commenting with your B.S trying to seem all knowledgeable, if you know how to do it answer it then. – user1602683 Jun 21 '13 at 21:29

5 Answers5

2

config/routes.rb

resources :posts

<% @posts.each do |post| %>
  <%= link_to "View post", post_path(post), :id => "posts", :class => "read-more" %>
<% end %>
ahmet
  • 4,955
  • 11
  • 39
  • 64
1

Your hash is missing a few params...

<% link_to "", { :controller => "posts", :action => "show", :id => post.id}, :id => "posts", :class => "read-more" %>

But I recommend

<% link_to "", post_path(post), :id => "posts", :class => "read-more" %>
Larry McKenzie
  • 3,253
  • 25
  • 22
0

I think what you are asking is this. You have an array or active record relation @posts which you want to show in a webpage with links to each post. If I'm right you can do

<% @posts.each do |post| %>
    <%= link_to "", { :controller => "posts" }, :id => post.id, :class => "read-more" %>br>
<% end %>

But you have to specify the action which will be triggered when the link is clicked.

0

I think this should work

<% @posts.each do |post| %>
    <%= link_to "", { :controller => "posts" , :action => :show}, :id => post.id, :class => "read-more" %>br>
<% end %> 
Grey
  • 676
  • 8
  • 23
0

How about this?

<% @posts.each do |post| %>
  <%= link_to "POST", post_path(post), :id => post.id %>
<% end %>
Jyothu
  • 3,104
  • 17
  • 26