ruby-on-railsrubybest-in-place

Rails best_in_place Unprocessable Entity


I'm trying to edit the name field of a model with the best_in_place gem to edit items directly in line. When trying to do so I'm getting a an error 422 Unprocessable Entity.

I've done some research and found out in the response the controller is expecting not only the name attribute but also the group and some other attributes.

["Group must exist","Lumo can't be blank","Lumo is not a number"]

I have setup my controller in the correct way (I think).

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

Is there a way with best_in_place to send these value's when updating the name attribute? I've tried with the params attribute from best_in_place.

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

This wasn't sending any extra params with the update.

Here's is what the Material model looks at.

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

Does anybody know why it's asking for other attributes and why best_in_place is not sending those along?


Solution

  • I figured out what the problem was and how to fix it. We use an option to Save or Save As when editing materials. In the controller we therefore check for the params[:commit].

    By editing the url I was able to send in the params[:commit] with the update with best_in_place. Here is how the best_in_place code ended up like:

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