无法使图像成为 link

Unable to make the image a link

我收到以下错误:

No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id]

这是我的代码

       <% @posts.each do |x| %>
         <div class="white">    
            <%= link_to image_tag x.image.url(:medium), post_path %>
                <p>Published <%= time_ago_in_words(x.created_at)%>ago</p>

         <br>

你有两个问题:

  • 您没有 post_path
  • 所需的参数
  • link_toimage_tag 参数越来越混乱

试试这个:

  <% @posts.each do |x| %>
     <div class="white">    
        <%= link_to image_tag(x.image.url(:medium)), post_path(x) %>
            <p>Published <%= time_ago_in_words(x.created_at)%>ago</p>

     <br>

Ruby 以原始方式(不带括号)解析嵌套调用的方式是这样的:

link_to image_tag(x.image.url(:medium), post_path(x))

当你明确的意思是:

link_to image_tag(x.image.url(:medium)), post_path(x)

当一个函数的参数停止而另一个函数的参数继续时,一组括号将告诉 Ruby。