link_to中如何写路由?

问题描述 投票:0回答:1

我使用活动存储和活动文本。我附上图像并写下文字。 Rails 创建带有图像和文本的帖子。主动存储创建的文件_blob.html.erb。我将 link_to 和路径添加到第 3 行:

<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
  <% if blob.representable? %>
    <%= link_to image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]), news_path(@new)%>
  <% end %>

  <figcaption class="attachment__caption">
    <% if caption = blob.try(:caption) %>
      <%= caption %>
    <% else %>
      <span class="attachment__name"><%= blob.filename %></span>
      <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
    <% end %>
  </figcaption>
</figure>

但是我单击图像时出现错误:

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

我想通过单击图像重定向以显示视图。如何解决这个问题?

ruby-on-rails rails-activestorage
1个回答
0
投票

我想通过单击图像重定向以显示视图。如何解决这个问题?

我认为这是正确的语法:

<%= link_to news_path(@new) do %>
  <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>

检查文档

但是我单击图像时出现错误...没有路线匹配...,缺少必需的键:[:id]

请确保您传递给

news_path
助手 (
@new
) 的对象确实有
id

例如在您上方添加此行

link_to
代码:

<% raise 'During debug I have found that my @new does not have id!!' if @new.id.nil? %>
© www.soinside.com 2019 - 2024. All rights reserved.