Ruby on rails Rails嵌套属性未使用carrierwave保存
我可以让画廊属性插入,正如下面的服务器日志所示,但图片属性也不会插入 服务器响应Ruby on rails Rails嵌套属性未使用carrierwave保存,ruby-on-rails,ruby-on-rails-5,carrierwave,Ruby On Rails,Ruby On Rails 5,Carrierwave,我可以让画廊属性插入,正如下面的服务器日志所示,但图片属性也不会插入 服务器响应 Started POST "/galleries" for 127.0.0.1 at 2017-05-13 18:19:23 +1000 Processing by GalleriesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEI
Started POST "/galleries" for 127.0.0.1 at 2017-05-13 18:19:23 +1000
Processing by GalleriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==", "gallery"=>{"name"=>"Hello", "cover"=>"123456", "picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50 @tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>, @original_filename="Skateboard 1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\"; filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"}
Unpermitted parameter: picture
(0.0ms) begin transaction
SQL (1.0ms) INSERT INTO "galleries" ("name", "cover", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Hello"], ["cover", 123456], ["created_at", 2017-05-13 08:19:23 UTC], ["updated_at", 2017-05-13 08:19:23 UTC]]
(65.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 74ms (ActiveRecord: 66.1ms)
Started GET "/" for 127.0.0.1 at 2017-05-13 18:19:23 +1000
....
2017年5月13日18:19:23+1000时,127.0.0.1版开始发布“/gallers”
GalleriesController处理#创建为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==”,“图库”=>{“名称”=>“你好”,“封面”=>“123456”,“图片”=>{“图片”=>},“提交”=>“创建图库”}
不允许的参数:图片
(0.0ms)开始事务处理
SQL(1.0ms)插入“图库”(“名称”、“封面”、“创建时间”、“更新时间”)值(?、、?、?)[“名称”、“您好”]、[“封面”,123456],“创建时间”,2017-05-13 08:19:23 UTC],“更新时间”,2017-05-13 08:19:23 UTC]]
(65.1ms)提交事务
重定向到http://localhost:3000/
74毫秒内完成302次(活动记录:66.1毫秒)
2017年5月13日18:19:23+1000开始获取127.0.0.1的“/”
....
厨房控制器
class GalleriesController < ApplicationController
def new
@gallery = Gallery.new
end
def create
@gallery = Gallery.new(gallery_params)
if @gallery.save
flash[:success] = "Picture created!"
redirect_to root_url
else
render 'new'
end
end
private
def gallery_params
params.require(:gallery).permit(:id, :name, :cover, pictures_attributes: [:id, :gallery_id, :picture, :_destroy])
end
end
class GalleriesController
_form.html.erb从new.html.erb中部分呈现
<%= form_for @gallery do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :cover %>
<%= f.text_field :cover %>
</div>
<div id="pictures">
<%= f.fields_for @gallery.pictures do |pic| %>
<%= pic.file_field :picture %>
</div>
<% end %>
<div id="submit">
<%= f.submit %>
</div>
<% end %>
模特、画廊
class Gallery < ApplicationRecord
has_many :pictures
validates :name, presence: true
validates :cover, presence: true
accepts_nested_attributes_for :pictures, allow_destroy: true
end
class CreateGalleries < ActiveRecord::Migration[5.0]
def change
create_table :galleries do |t|
t.string :name
t.integer :cover
t.timestamps
end
end
end
类库
图画
class Picture < ApplicationRecord
belongs_to :gallery
validates :gallery_id, presence: true
validates :picture, presence: true
mount_uploader :picture, PictureUploader
serialize :picture, JSON
end
class CreatePictures < ActiveRecord::Migration[5.0]
def change
create_table :pictures do |t|
t.integer :gallery_id
t.string :picture
t.timestamps
end
end
end
类图片
迁移,画廊
class Gallery < ApplicationRecord
has_many :pictures
validates :name, presence: true
validates :cover, presence: true
accepts_nested_attributes_for :pictures, allow_destroy: true
end
class CreateGalleries < ActiveRecord::Migration[5.0]
def change
create_table :galleries do |t|
t.string :name
t.integer :cover
t.timestamps
end
end
end
class CreateGalleries
图画
class Picture < ApplicationRecord
belongs_to :gallery
validates :gallery_id, presence: true
validates :picture, presence: true
mount_uploader :picture, PictureUploader
serialize :picture, JSON
end
class CreatePictures < ActiveRecord::Migration[5.0]
def change
create_table :pictures do |t|
t.integer :gallery_id
t.string :picture
t.timestamps
end
end
end
class CreatePictures
根据此处的参数行判断:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==",
"gallery"=>{"name"=>"Hello", "cover"=>"123456",
"picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50
@tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>,
@original_filename="Skateboard 1.jpg", @content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\";
filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"}
从这里的参数行判断:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==",
"gallery"=>{"name"=>"Hello", "cover"=>"123456",
"picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50
@tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>,
@original_filename="Skateboard 1.jpg", @content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\";
filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"}
不允许的参数:图片
错误是因为的字段\u错误。的第一个参数应该是一个记录名称
(在您的情况下,它应该是:pictures
)
字段(记录名称,记录对象=nil,选项={},&block)
您正在将record\u object
作为第一个参数传递,这将导致错误的参数,并导致不允许的错误。将代码更改为以下应该可以解决此问题
<%= f.fields_for :pictures, @gallery.pictures do |pic| %>
<%= pic.file_field :picture %>
<% end %>
不允许的参数:图片
错误是因为
的字段\u错误。的第一个参数应该是一个记录名称
(在您的情况下,它应该是:pictures
)
字段(记录名称,记录对象=nil,选项={},&block)
您正在将record\u object
作为第一个参数传递,这将导致错误的参数,并导致不允许的错误。将代码更改为以下应该可以解决此问题
<%= f.fields_for :pictures, @gallery.pictures do |pic| %>
<%= pic.file_field :picture %>
<% end %>
是的,但我认为这是对图片属性的说法,所以错误一定在其他地方?@leeether您当前允许参数hashpictures\u attributes
,但是您正在提交参数hashpicture
。我试过了,我得到了一张画廊的未知属性错误图片
我很确定您必须将图片的属性指定为表单中参数的picture\u属性
,即使只是添加图片
模型属性没有特别引用它们仍然不起作用,所以一定是其他的东西,但我认为它说的是图片属性,所以错误一定在其他地方?@leeether您当前允许参数hashpictures\u attributes
,但是您正在提交参数hashpicture
。我试过了,我得到了一张画廊的未知属性错误图片
我很确定您必须将图片的属性指定为表单中参数的picture\u属性
,即使只是添加图片
模型属性如果没有特别引用它们,仍然无法工作,因此必须有其他东西供您输入。你是对的,但当我这样做时,页面上图片的文件\u字段
消失了,因此我必须为@gallery.pictures做f.fields\u。
现在我意识到要获得允许的参数,我需要将字段命名为名称:“pictures[picture]”
这会消除错误,但只会插入图库属性,而不会插入图片。所以要在控制器中处理这个问题,我需要添加params[:pictures][:picture]。每个do | pic |
然后@picture=@gallery.pictures.create(picture:pic)
end
和所有作品:)谢谢Guysorry,实际上产生错误的只是@picture=@gallery.pictures.create(picture:params[:pictures][:picture])
因为它是一个单独的图片,所以可以为您输入pavan。您是对的,但是当我这样做时,页面上的图片的文件字段
消失了,所以我必须为@gallery做f.fields\u。