Ruby on rails 更新的ruby和rails给.new.update方法带来了错误
我最近更新了我的ruby版本和rails版本Ruby on rails 更新的ruby和rails给.new.update方法带来了错误,ruby-on-rails,ruby,ruby-on-rails-4,crud,Ruby On Rails,Ruby,Ruby On Rails 4,Crud,我最近更新了我的ruby版本和rails版本 ruby 1.9.3 to ruby 2.1.1 rails 3.2.6 to rails 4.0.0 然后安装以下新gems protected_attributes(1.0.3) turbolinks(2.5.3) 在运行代码时,我在创建和更新方法时遇到以下错误 获取的参数数错误(2对1)错误位于.new、.update方法。 例如 我从表格中发送以下参数 Parameters: {"utf8"=>"✓", "authentic
ruby 1.9.3 to ruby 2.1.1
rails 3.2.6 to rails 4.0.0
然后安装以下新gems
protected_attributes(1.0.3)
turbolinks(2.5.3)
在运行代码时,我在创建和更新方法时遇到以下错误
获取的参数数错误(2对1)错误位于.new、.update方法。
例如
我从表格中发送以下参数
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4DosQk69bQzV9idZapxjseVPNedORytYtNYH4rUCeBk=", "gallery"=>{"title"=>"test title 10", "description"=>"this is the gallery desription", "user_id"=>"1", "images_attributes"=>{"0"=>{"title"=>"test image", "description"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0xb48788b8 @tempfile=#<Tempfile:/tmp/RackMultipart20160229-7649-3kw561>, @original_filename="564650_685411374823853_181629729_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[images_attributes][0][image]\"; filename=\"564650_685411374823853_181629729_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, "user_id"=>"1"}}}, "commit"=>"Update", "locale"=>"en"}
试试这个:
def gallery_params
params.require(:gallery).permit(:title, :user_id, :description, images_attributes: [:title, :description, :image, :user_id])
end
一旦尝试这样做,让我们看看问题是否存在于强参数中
def create
@gallery = Gallery.new({"title"=>"test title 10", "description"=>"this is the gallery desription", "user_id"=>"1", "images_attributes"=>{"0"=>{"title"=>"test image", "description"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0xb48788b8 @tempfile=#<Tempfile:/tmp/RackMultipart20160229-7649-3kw561>, @original_filename="564650_685411374823853_181629729_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[images_attributes][0][image]\"; filename=\"564650_685411374823853_181629729_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, "user_id"=>"1"}}})
respond_to do |format|
if @gallery.save
format.html { redirect_to :back, :notice => t('notice.gallery_created') }
format.json { render json: @gallery, status: :created, location: @gallery }
else
format.html { render action: "new" }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def创建
@gallery=gallery.new({“title”=>“test title 10”,“description”=>“这是gallery描述”,“user\u id”=>“1”,“images\u attributes”=>{“0”=>{“title”=>“test image”,“description”=>,“image”=>,“user\u id”=>“1}})
回应待办事项|格式|
如果@gallery.save
format.html{redirect_to:back,:notice=>t('notice.gallery_created')}
format.json{render json:@gallery,status::created,location:@gallery}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@gallery.errors,status::unprocessable_entity}
结束
结束
结束
很抱歉,这没有帮助我检查gem文件中的rails版本以及您发送的参数?我给出了答案,因为在注释中写入内容太大了,可能是不兼容的gem-完整错误回溯可能会有帮助。此外,我还认为您最好至少更新到最新的4.0.x版本,而不是4.0.0(即使这样,您仍然会有未修补的安全性更改)oks将更新到最新的4.0版本,并再次检查,感谢您在第行和Gallery上方的提示。新建(:title=>“test title 10”,:description=>“这是Gallery描述”,:user_id=>“1”),仍然给出相同的结果
def gallery_params
params.require(:gallery).permit(:title, :user_id, :description, images_attributes: [:title, :description, :image, :user_id])
end
def create
@gallery = Gallery.new({"title"=>"test title 10", "description"=>"this is the gallery desription", "user_id"=>"1", "images_attributes"=>{"0"=>{"title"=>"test image", "description"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0xb48788b8 @tempfile=#<Tempfile:/tmp/RackMultipart20160229-7649-3kw561>, @original_filename="564650_685411374823853_181629729_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[images_attributes][0][image]\"; filename=\"564650_685411374823853_181629729_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, "user_id"=>"1"}}})
respond_to do |format|
if @gallery.save
format.html { redirect_to :back, :notice => t('notice.gallery_created') }
format.json { render json: @gallery, status: :created, location: @gallery }
else
format.html { render action: "new" }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end