ruby-on-rails-4 – 为什么当我在rails 4中使用carrierwave上传
发布时间:2020-12-17 04:01:34 所属栏目:百科 来源:网络整理
导读:我尝试在rails 4中使用carrierwave保存图像文件,但是当提交按钮时单击数据库是否回滚???为什么? 如果我不选择图像上传,只发送字符串,所有工作正常但如果我发送图像文件我得到这样的错误: TypeError: can't cast ActionDispatch::Http::UploadedFile to str
我尝试在rails 4中使用carrierwave保存图像文件,但是当提交按钮时单击数据库是否回滚???为什么?
如果我不选择图像上传,只发送字符串,所有工作正常但如果我发送图像文件我得到这样的错误: TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "items" ("created_at","description","image","name","store_id","sub_items","title","updated_at") VALUES (?,?,?) (0.2ms) rollback transaction *** ActiveRecord::StatementInvalid Exception: TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "items" ("created_at",?) 我的看法 : <%= form_for @items,:url => dashboard_create_items_path(@store.id),:html => {:multipart => true} do |f| %> <%= f.label :name,'Name' %> <%= f.text_field :name %><br /> <%= f.label :title,'Title' %> <%= f.text_field :title %><br /> <%= f.label :image,'Image' %> <%= f.file_field :image %><br /> <%= f.label :description,'Description' %> <%= f.text_field :description %><br /> <%= f.label :sub_items %><br> <%= f.select :sub_items,options_for_select([["true",true],["false",false]]),:selected => 'true' %><br /> <%= f.submit %> <% end %> 我的控制器: def create_items store = Store.find(params[:id]) @items = store.items.create(item_params) if @items redirect_to dashboard_show_items_url(@items.id,store.id) else redirect_to dashboard_new_items_url end end private def item_params params.require(:item).permit(:name,:title,:image,:description,:sub_items,:store_id) end 我什么时候错了请告诉我?谢谢你 解决方法
您可能会看到此错误消息,因为Carrierwave尚未在您的模型中初始化.
需要将carrierwave方法mount_uploader添加到模型中的image字段,如下所示: class Item < ActiveRecord::Base mount_uploader :image,ImageUploader end (有关详细信息,请参阅docs) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |