首页 > 解决方案 > 文章中的参数错误

问题描述

将图像上传到页面后,我在 Articles#show 视图中收到此 ArgumentError。Can't resolve image into URL: undefined method `to_model' for # 你的意思是?to_yaml

<% if @article.photo.present? %>
<%= image_tag(@article.photo, style:"width:100%") %>
<% else %>
<%= image_tag 'banner.png', style:"width:100%" %>
<% end %>

这也是我在文章控制器中的私有方法:

private
  def article_params
    params.require(:article).permit(:title, :text, :search, :music, :movie, :photo)
  end

标签: ruby-on-rails

解决方案


因为你使用回形针,这条线是你的错误的问题

<%= image_tag(@article.photo, style:"width:100%") %>

您可以在您的文章模型(app/models/article.rb)中检查回形针中的格式设置,通常它形成为缩略图、中等、原始,您可以使用下面的示例编写代码

<%= image_tag(@article.photo.url(:original), class: "thumb-style") %>

在你的 CSS 文件中

.thumb-style {
  width: 100%;
}

推荐阅读