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 in PG based on the checkboxes values. However, when editing, the checkboxes do NOT load the correct values from the database. They all appear unchecked. The same thing happens when creating even with the default values as 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
Aucun commentaire:
Enregistrer un commentaire