1

My app has merchants that can accept 3 methods of payment. In order to create / edit the records I am using multiple checkboxes in a form and an array field in PostgreSQL

The funny thing is that it works fine when saving the values, both when creating and updating. The array is saved fine to the PG table as an array based on the checkboxes values. However, when editing, the edit form DOES NOT load the correct values from the database. All the checkboxes always appear unchecked, disregarding the array values in the PG table. The same thing happens when creating even though the migration sets the default values as all checkboxes supposedly checked.

Any help would be appreciated.

Shared view partial for create and edit:

= form_for @merchant, layout: :horizontal do |f|
  = f.check_box :payment_accepted, { :multiple => true }, 'Credit Card', nil
  = f.check_box :payment_accepted, { :multiple => true }, 'Paypal', nil
  = f.check_box :payment_accepted, { :multiple => true }, 'Direct Deposit', nil
  = f.submit 'Save Changes', :class => 'btn btn-primary'

My controller code is:

class MerchantsController < ApplicationController
  load_and_authorize_resource
  before_action :set_merchant, only: [:show, :edit, :update, :destroy]

  respond_to :html, :json, :js

  def index
    @merchants = Merchant.all
  end 

  def show
  end 

  def new 
    @merchant = Merchant.new
  end 

  def edit
  end 

  def create
    @merchant = Merchant.new(merchant_params)
    @merchant.save
    respond_with(@merchant)
  end 

  def update
    @merchant.update(merchant_params)
    flash[:notice] = 'Merchant was successfully updated.'
    respond_with(@merchant)
  end 

  def destroy
    @merchant.destroy
    redirect_to merchants_url, notice: 'Merchant was successfully destroyed.'
  end 

  private
    def set_merchant
      @merchant = Merchant.find(params[:id])
    end 

    def merchant_params
      params.require(:merchant).permit(:name, :description, :notif_email, :category_id, :country_id, :costo_procesamiento, payment_accepted:[]) 
    end 
end

And my migration:

class CreateMerchants < ActiveRecord::Migration
  def change
    create_table :merchants do |t|
      t.string :name
      t.text :description
      t.text :default_terms
      t.text :notif_email
      t.boolean :visible, default: true
      t.integer :category_id
      t.integer :country_id
      t.integer :costo_procesamiento, null:false, default: 0
      t.string :payment_accepted, array: true, default: "['Credit Card','Paypal','Direct Deposit']"
      t.references :country, index: true, foreign_key: true
      t.timestamps null: false
    end
  end
end

1 Answer 1

2

This is because while the checkboxes know there are multiple values, they don't know which is which (they can't go off the checked/unchecked values). Change your checkboxes so they know when they should or shouldn't be checked:

= f.check_box :payment_accepted, { :multiple => true, checked: @payments.include?('Credit Card')}, 'Credit Card', nil
= f.check_box :payment_accepted, { :multiple => true, checked: @payments.include?('Paypal') }, 'Paypal', nil
= f.check_box :payment_accepted, { :multiple => true, checked: @payments.include?('Direct Deposit') }, 'Direct Deposit', nil

and in your controller, add:

def edit
  @payments = @merchant.payment_accepted
end 
Sign up to request clarification or add additional context in comments.

1 Comment

This made my day ! I needed checked attr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.