2017-04-10 25 views
0

我在编辑best_in_place gem模型的名称字段以直接编辑项目。当试图这样做时,我得到一个错误422不可处理的实体。Rails best_in_place无法处理的实体

我已经做了一些研究,并在响应中发现,控制器不仅期望name属性,还包括组和其他一些属性。

["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 

有没有一种方式与best_in_place更新名称属性时发送这些值?我尝试了best_in_place的params属性。

<%= best_in_place @material, :name, as: :input, params: { group_id: @material.group_id } %> 

这并没有发送任何额外的参数与更新。

以下是Material模型的内容。

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

我想出了问题所在,以及如何解决问题。编辑材料时,我们使用选项“保存”或“另存为”。因此我们在控制器中检查params[:commit]

通过编辑网址,我可以发送params[:commit],并使用best_in_place进行更新。这里是best_in_place代码如何结束如下:

<%= best_in_place @material, :name, as: :input, url: material_path(@material, commit: "Save") %>