Search code examples
ruby-on-railsrubyactioncontroller

Why am I getting an undefined method `deep_merge' for ActionController::Parameters error?


In my Rails 5 app I am trying to do this in a controller:

  def create
    company_params = params.require(:company).permit(
      :name,
      :email,
      :people_attributes => [
        :first_name,
        :last_name
      ]
    ).deep_merge(
      :creator_id => current_user.id,
      :people_attributes => [
        :creator_id => current_user.id
      ]
    )
    @company = current_account.companies.build(company_params)
    if @company.save
      flash[:success] = "Company created."
      redirect_to companies_path
    else
      render :new
    end
  end

For some reason I am getting this error, though:

undefined method `deep_merge' for ActionController::Parameters:0x007fa24c39cfb0

What am I missing here?


Solution

  • OK, I couldn't get deep_merge to work, so I ended up doing this:

      def create
        company_params = params.require(:company).permit(
          :name, 
          :email,
          :people_attributes => [
            :first_name,
            :last_name
          ]
        )
        @company = current_account.companies.build(company_params)
        @company.creator = current_user
        @person = @company.people.last
        @person.account = current_account
        @person.creator = current_user
        if @company.save
          flash[:success] = "Company created."
          redirect_to companies_path
        else
          render :new
        end
      end
    

    Not sure if this is good or bad practice. Feedback appreciated!