我正在为##获取未定义的方法`file'

时间:2014-08-02 17:01:30

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave form-for

我正在尝试进行图片上传,过去我使用了carrierwave并且发现它很直接。这一次,我没有使用有效记录。

这是我的模特:

class Garment
    include ActiveAttr::Model
    extend CarrierWave::Mount

    attribute :alternativeColour
    attribute :title
    attribute :image
    attribute :image2
    attribute :image3
    attribute :image4
    attribute :image5
    attribute :image6
    attribute :price
    attribute :favourite
    attribute :recommended
    attribute :gender
    attribute :productType
    attribute :size
    attribute :colour 
    attribute :collection

    mount_uploader :image, ImageUploader

end

我的控制器:

class AdminpanelController < ApplicationController

  def index        
  end

  def new
    @garment = Garment.new
  end

  def create
    garment = Parse::Object.new("Garments")
    garment["title"] = params[:garment][:title]
    garment["price"] = params[:garment][:price].to_f
    garment["alternativeColour"] = params[:garment][:alternativeColour].to_bool
    garment["gender"] = params[:garment][:gender].to_i
    garment["recommended"] = params[:garment][:recommended].to_bool
    garment["productType"] = params[:garment][:productType].to_i
    garment["size"] = params[:garment][:size].to_i
    garment["colour"] = params[:garment][:colour].to_i
    garment["collection"] = params[:garment][:collection].to_i
    garment["image"] = params[:garment][:image]
    garment.save

    redirect_to adminpanel_index_path
  end
end

我的观点(取出一些节省空间的代码):

<%= form_for :garment, :url => adminpanel_index_path, :html => {:multipart => true} do |f| %>
  <%= f.text_field :title, :placeholder => "Title" %> <br \>
  <%= f.text_field :price, :placeholder => "Price" %> <br \>
  <%= f.file :image %>
  <%= f.submit "Create" %> <br \>
<% end %>

为什么我会收到错误?

enter image description here

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

ActionView::Helpers::FormHelper中没有这样的方法f.file这会引发undefined method file for #<ActionView::Helpers::FormBuilder。您应该在表单中使用f.file_field

file_field(object_name, method, options = {})

<%= f.file_field :image %>