Ruby on rails I';m获取未定义的方法'file';对于#<;尝试在没有activerecord的情况下使用carrierwave时,ActionView::Helpers::FormBuilder
我尝试上传图像,过去我使用carrierwave,发现它非常简单。这次我不使用活动记录 这是我的型号:Ruby on rails I';m获取未定义的方法'file';对于#<;尝试在没有activerecord的情况下使用carrierwave时,ActionView::Helpers::FormBuilder,ruby-on-rails,ruby,ruby-on-rails-4,carrierwave,form-for,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
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 %>
我的控制器:
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 %>
class AdminpanelController
我的视图(取出一些代码以节省空间):
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 %>
adminpanel_index_path,:html=>{:multipart=>true}do | f |%>
“标题”%>
“价格”%>
为什么我会出错
谢谢您的时间。在
ActionView::Helpers::FormHelper
中,没有这样的方法f.file
此方法文件用于您的表单中的#:
file_field(object_name, method, options = {})
<%= f.file_field :image %>
文件\字段(对象\名称、方法、选项={})