【发布时间】:2016-06-12 12:41:25
【问题描述】:
我最近更新了我的 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)
在运行代码时,创建和更新方法时出现以下错误。
.new, .update 方法出现参数数量错误(2 比 1) 错误。 例如。
**wrong number of arguments (2 for 1)**
def create
**@gallery = Gallery.new(gallery_params)**
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
private
def gallery_params
params.require(:gallery).permit(:title, :user_id, :description, images_attributes: [:title, :description, :image, :user_id])
end
我正在从我的表单中发送以下参数。
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"}
我的应用程序中的每个 表单(form_for 或 nested_form_for)都会出错。
在控制器中尝试了以下代码来保存我的数据,但 .new .update 仍然存在问题。 例如。
@gallery = Gallery.new
@gallery.title = "test title 10"
@gallery.description = "this is the gallery description"
@gallery.user_id = 1
@gallery.save
【问题讨论】:
-
检查你的gem文件中的rails版本
-
您要发送的参数?
-
我已经给出了答案,因为写在评论中会很大
-
可能是不兼容的 gem - 完整的错误回溯可能会有所帮助。另外,我原以为您最好至少更新到最新的 4.0.x 版本而不是 4.0.0(即使那样,您仍然会有未修补的安全更改)
-
oks 将更新到最近的 4.0 版本并再次检查谢谢
标签: ruby-on-rails ruby ruby-on-rails-4 crud