简体   繁体   English

Ruby on Rails链接_至

[英]Ruby on Rails link_to

I am using link_to with some HTML elements and this is what I have : <% link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %> 我正在将link_to与一些HTML元素一起使用,这就是我所拥有的: <% 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. 但我希望它将链接到每个帖子具有的ID的帖子,任何帮助将不胜感激。

Thank You 谢谢

config/routes.rb config / routes.rb

resources :posts

<% @posts.each do |post| %>
  <%= link_to "View post", post_path(post), :id => "posts", :class => "read-more" %>
<% end %>

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" %>

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. 您有一个数组或活动记录关系@posts,您希望在包含每个帖子链接的网页中显示它们。 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. 但是,您必须指定单击链接时将触发的操作。

I think this should work 我认为这应该工作

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

How about this? 这个怎么样?

<% @posts.each do |post| %>
  <%= link_to "POST", post_path(post), :id => post.id %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM