簡體   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