我正在尝试使用best_in_place gem编辑模型的名称字段,以直接编辑项目。尝试这样做时,我得到一个错误422 Unprocessable Entity。
我做了一些研究,并在响应中发现控制器不仅期望名称属性,还期望组和其他一些属性。
["Group must exist","Lumo can't be blank","Lumo is not a number"]
我已经以正确的方式设置了我的控制器(我认为)。
materials_controller.rb
def update
@material = Material.find(params[:id])
if params[:commit] == "Save" then
success = @material.update(material_params)
params[:create_pure_composition] = false
else
@material = Material.new(material_params)
@material.user_id = current_user.id
success = @material.save
end
respond_to do |format|
if success
plot1 = prepare_e_plot(@material)
plot2 = prepare_st_plot(@material)
format.html { redirect_to @material }
format.json { render json: { plot1: plot1, plot2: plot2, status: 200 } }
else
format.html { render 'edit' }
format.json { respond_with_bip(@material) }
end
end
end
更新name属性时,有没有办法让best_in_place发送这些值?我尝试过best_in_place的params属性。
<%= best_in_place @material, :name, as: :input, params: { group_id: @material.group_id } %>
这并没有随更新发送任何额外的参数。
以下是材料模型的内容。
material.rb
class Material < ActiveRecord::Base
belongs_to :user
belongs_to :group
validates :name, presence: true, uniqueness: {scope: :user_id}
validates :lumo, presence: true, numericality: true
end
有人知道为什么要求其他属性以及为什么best_in_place不会发送这些属性?
答案 0 :(得分:0)
我弄清楚问题是什么以及如何解决问题。编辑材料时,我们使用“保存”或“另存为”选项。因此,在控制器中,我们检查params[:commit]
。
通过编辑我能够使用best_in_place在params[:commit]
中发送更新的网址。以下是best_in_place代码最终的结果:
<%= best_in_place @material, :name, as: :input, url: material_path(@material, commit: "Save") %>