繁体   English   中英

Ruby on Rails链接_至

[英]Ruby on Rails link_to

我正在将link_to与一些HTML元素一起使用,这就是我所拥有的: <% link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %>

但我希望它将链接到每个帖子具有的ID的帖子,任何帮助将不胜感激。

谢谢

config / routes.rb

resources :posts

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

您的哈希值缺少一些参数...

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

但我建议

<% link_to "", post_path(post), :id => "posts", :class => "read-more" %>

我想你在问的是这个。 您有一个数组或活动记录关系@posts,您希望在包含每个帖子链接的网页中显示它们。 如果我是对的,你可以做

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

但是,您必须指定单击链接时将触发的操作。

我认为这应该工作

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

这个怎么样?

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