【问题标题】:Carrierwave NoMethodError: undefined method `image_url' for ItemCarrierwave NoMethodError:项目的未定义方法“image_url”
【发布时间】:2014-03-17 20:52:57
【问题描述】:

请帮忙。两天安装 Carrierwave,但因错误而停止: 显示 /home/dima/rails_projects/my_store/app/views/items/show.html.erb 其中第 8 行出现:undefined method `image_url' for # items_controller.rb:

 def show
    unless @item 
      render text: "Page not here", status: 404
    end
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.create(item_params)
    if @item.errors.empty?
      redirect_to item_path(@item)
    else
      render "new"
    end
  end

  def update
    @item.update_attributes(item_params)
    if @item.errors.empty?
      redirect_to item_path(@item)
    else
      render "edit"
    end
  end

    def item_params
      params.require(:item).permit(:name, :description, :price, :weight, :real, :image_attributes)
    end
end

show.html.erb:

<h1><%= @item.name%></h1>
<ul>
    <li>Цена: <%= @item.price %> грн.</li>
   <li>Описание: <%= urls_to_links(urls_to_images(@item.description)) %></li>
   <li>Вес: <%= @item.weight %> кг</li>
   <li>Реальный: <%= @item.real %></li>
   <%= image_tag @item.image_url.to_s %>
 </ul>

item.rb

class Item < ActiveRecord::Base

  validates :price, numericality: { greater_than: 0, allow_nil: true } 
  validates :name, :description, presence: true

  #  has_one :image
  # accepts_nested_attributes_for :image
end

图像.rb

class Image < ActiveRecord::Base
  mount_uploader :image, ImageUploader
  # belongs_to :imageable, polymorphic: true
end

new.html.erb

<h1>Create new item</h1>
<%= form_for @item, :html => {:multipart => true} do |f| %>
  <%= render partial: "form", locals: { f: f } %>
  <p><%= f.submit "Создать товар" %></p>
<% end %>

_form.html.erb

<p>Наименоваие: <%= f.text_field :name %></p>
<p>Описание: <%= f.text_field :description %></p>
<p>Цена: <%= f.text_field :price %></p>
<p>Вес: <%= f.text_field :weight %></p>
<p>Реальный: <%= f.text_field :real %></p>
<label>My Image</label>
<p><%= f.file_field :image %></p>

尝试了不同的变体(比如没有'#')。请帮助学习 Rails。 (上次由截屏http://railscasts.com/episodes/253-carrierwave-file-uploads安装)

更新: 如果我使用

    def item_params
      params.require(:item).permit(:name, :description, :price, :weight, :real, :image)
    end

有一个错误: TypeError:无法将 ActionDispatch::Http::UploadedFile 转换为字符串:INSERT INTO “items”(“created_at”、“description”、“image”、“name”、“price”、“updated_at”、“weight”)值(?,?,?,?,?,?,?)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 carrierwave


    【解决方案1】:

    假设has_one :image是从Item到Image的引用,那么要引用你应该使用的图片路径:

    <%= image_tag @item.image.image.url if @item.image %>
    

    代替您当前的标签:。

    此外,您的表单的编写方式好像 mount_uploader :image 是在 Item 模型上定义的,但是您的模型定义表明它首先必须通过 Image 模型才能获取载波图像。也许只是将图像直接安装在项目上?

    【讨论】:

    • 我收到一个错误:NoMethodError in Items#show Showing /home/dima/rails_projects/my_store/app/views/items/show.html.erb where line #8 raise: undefined method `image'对于 nil:NilClass
    • 这可能是因为item没有关联的图像模型,所以活动记录关系是空白的。我已经更新了答案。
    • 有同样的错误。我应该取消注释“belongs_to”和“has_one”吗?
    • 现在图像没有在 +0200 处为 127.0.0.1 保存 Started POST "/items" 由 ItemsController#create 作为 HTML 参数:{"utf8"=>"✓", "authenticity_token"= >"ans=", "item"=>{"name"=>"12121", "description"=>"212121", "price"=>"1212", "weight"=>"12", "real "=>"", "image"=>#<:http::uploadedfile:0xb6396dc0>, @original_filename="vim.png", @content_type ="image/png", @headers="Content- Disposition: form-data; name=\"item[image]\"; filename=\"vim.png\"\r\nContent- Type: image/png\ r\n">}, "commit"=>"Создать товар"}
    • 未经允许的参数:图像 (0.1ms) 开始事务 SQL (1.9ms) INSERT INTO "items" ("created_at", "description", "name", "price", "updated_at", " weight") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Mon, 17 Mar 2014 21:31:59 UTC +00:00], ["description", "212121"], ["name", "12121"], ["price", 1212.0], ["updated_at", Mon, 17 Mar 2014 21:31:59 UTC +00:00], ["weight", 12.0]] (39.6 ms) 提交事务重定向到localhost:3000/items/24
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多