“#”的未定义方法“url”:字符串

2024-03-30

我使用Carrierwave上传图片,上传后,转到显示页面时出现错误:

undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x007f8b6134d610>":String
<%= image_tag @product.picture.url if @product.picture? %> 

这是我的代码:

_form.html.erb

<div class="picture">
    <%= f.file_field :picture %>
  </div>

产品.rb

class Product < ApplicationRecord
  has_many :reviews, dependent: :destroy
  mount_uploader :picture, PictureUploader  
end

显示.html.erb

<p id="notice"><%= notice %></p>
<p>
  <strong>Name:</strong>
  <%= @product.name %>
</p>
  <strong>Picture:</strong>
  <%= image_tag @product.picture.url if @product.picture? %>
</p>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>

有人知道如何解决这个问题吗?

Update:

图片上传器.rb

# encoding: utf-8

class PictureUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

这是 Gemfile 中的版本:

gem 'carrierwave',             '0.11.2'
gem 'mini_magick',             '4.5.1'
gem 'fog',                     '1.38.0'

我会首先检查强参数并添加:picture属性(如果尚未添加)。

然后我会尝试将其添加到您的显示视图中:

显示.html.erb

<%= image_tag(@product.picture_url.to_s) %>

而不是这行代码:

<%= image_tag @product.picture.url if @product.picture? %>

如果您遇到权限问题,您可以创建配置文件:

config/initializers/carrierwave.rb

并添加权限:

CarrierWave.configure do |config|
  config.permissions = 0666
  config.directory_permissions = 0777
  config.storage = :file
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“#”的未定义方法“url”:字符串 的相关文章

  • Rails、REST 架构和 HTML 5:带有预检请求的跨域请求

    在致力于使我们的网站 HTML 5 友好的项目时 我们渴望采用跨域请求的新方法 不再通过隐藏的 iframe 发布 使用访问控制 http www w3 org TR access control 根据规范 我们开始设置一些测试来验证各种浏
  • 多重要求和允许强参数rails 4

    在下面的情况下 我尝试使用强参数 我想要求email address password并允许remember me fields 但像下面这样使用它只允许最后一行在方法示例中 在下面的情况下 它只需要params permit rememb
  • 如何使用 Rspec 测试具有嵌套路由的控制器?

    我有 2 个使用轨道脚手架生成器创建的控制器 我希望它们嵌套在一个名为 demo 的文件夹中 所以运行 rails g scaffold demo flows rails g scaffold demo nodes 然后我决定将节点嵌套在流
  • Rails/Nginx 中的超时——最佳实践

    我正在开发一个应该在 Nginx 服务器上运行的 Rails 应用程序 根据输入 应用程序可能需要很长时间来处理请求 或者在出现错误时挂起 因此我想防止进程永远运行 除了确保客户端收到超时信号的 Nginx 配置之外 我想我可能仍然需要确保
  • 使用 Ruby aws-sdk 跟踪文件到 S3 的上传进度

    首先 我知道SO中有很多与此类似的问题 在过去的一周里 我读了大部分 如果不是全部 但我仍然无法让这项工作为我工作 我正在开发一个 Ruby on Rails 应用程序 允许用户将 mp3 文件上传到 Amazon S3 上传本身工作正常
  • 即使在急切加载之后,belongs_to 关联也会单独加载

    我有以下关联 class Picture lt ActiveRecord Base belongs to user end class User lt ActiveRecord Base has many pictures end 在我的
  • Windows 7 x64 上的 Ruby on Rails SQLite 问题

    当我尝试做的时候db create使用 Windows 7 x64 在具有此数据库配置的 sqlite 数据库上 development adapter sqlite3 database db development sqlite3 tim
  • Ruby on Rails 服务器在 HTTPS POST 请求期间崩溃

    我正在尝试与你沟通城市飞艇API http urbanairship com docs push html broadcast使用 ROR Web 应用程序 在我的控制器中 我有以下代码 require net http require n
  • imagemagick 不会删除 tmp 文件

    我有一个使用 Carrierwave S3 和 rmagci 的应用程序 或多或少以下http railscasts com episodes 253 rierwave file uploads http railscasts com ep
  • 仅针对 Rake 任务运行初始化程序

    我希望在执行 Rake 任务时运行某个初始化程序 但在运行 Rails 服务器时不运行 区分 Rake 调用和服务器调用的最佳方法是什么 Rake 允许您指定任务的依赖关系 最好的建议操作是将特定于 rake 的初始化放入其自己的任务中 而
  • Rails 3:如何让用户在表单中至少选择一个复选框?

    我有一个表单 用户在提交表单之前必须选中至少一个复选框 有没有任何插件可以处理这个问题或者jquery可以应用于我的表单 不幸的是我完全是一个 jquery 菜鸟 上面的所有答案都显示了如何在客户端执行此操作 这确实可能更好 正如您的问题标
  • Travis-CI 上的 Rails 数据库设置

    我正在尝试在 Rails 项目上使用 Travis 持续集成 文档说 对于 SQLite3 测试数据库必须配置如下 test adapter sqlite3 database memory timeout 500 但我想保留本地测试的默认配
  • 在 Rails 中本地化嵌套虚拟属性

    怎么可能本地化嵌套虚拟属性在 Rails 中 该模型 class User lt ActiveRecord Base attr accessor company information This is used in callbacks e
  • 使用 AJAX 和 JQuery 按设定的时间间隔刷新 Rails 部分

    I have a page in my rails application that looks like 现在 我有另一个用 python 编码的人工智能应用程序 它处理视频 显示在 Rails 应用程序页面的左侧 并使用捕获的车辆及其相
  • 如何根据域名更改视图格式

    我想知道是否有任何方法可以根据域名更改相同 Rails 应用程序的视图格式 例如 www domain com gt respond to format html api domain com gt respond to format xm
  • 如何在Rails中使用jquery tokeninput?

    我正在尝试在我的应用程序的表单中添加 tokeninput jquery 字段 该字段允许用户发布状态更新 我希望用户能够将作品 单独的模型 附加到状态更新中 我正在使用 act as taggable on gem 并且我的查询指定标签上
  • 从 Ruby on Rails 应用程序运行 phantomjs

    我有兴趣使用幻影 http www phantomjs org 我想从我的 Ruby on Rails 应用程序运行它 然而 这是一个命令行工具 即我需要运行类似phantomjs rasterize js http raphaeljs c
  • Rails 的多个数据库不适用于远程数据库

    我有一个远程只读 postgres 数据库 它是通过 docker 实例维护的卡尔达诺数据库同步 https github com input output hk cardano db sync 我设法将开发数据库连接到它 它工作正常 但由
  • 将对象转换为哈希,然后将其保存到用户的列

    找不到任何接近我想做的事情 我想将一个对象存储到用户的列中 该列采用数组的形式 postgres def change add column users interest string array true default end 我有另一
  • 如何在 Rails 中获取“应用程序跟踪”而不是完整的详细回溯?

    在开发环境的Rails默认错误页面中 您可以查看三个回溯 1 Application Trace 2 Framework Trace和3 Full Trace 但是如何在 Rails 控制器中获取 应用程序跟踪 完整跟踪 exception

随机推荐