lundi 26 janvier 2015

Rails 4: Creating an update form for "belongs_to" object using checkboxes

I'm totally stumped on what seems like it should be the most basic of issues!


I have an app that draws data out of a Salesforce database and I am trying to create an admin panel where the user can select (via checkboxes) the various objects and fields they want the app to use.


The ideal setup has each object in the header of a bootstrap accordion - including a checkbox to 'whitelist the object' and then on a click it reveals a list of it's fields, also with checkboxes to 'whitelist the field'



-Object 1
--Object 1- Field 1
--Object 1- Field 2
--Object 1- Field 3
--Object 1- Field 4
-Object 2
-Object 3


I've even rigged this up with some distinctly un-railsy and un-DRY code... but I couldn't work out how to connect the action of checking and unchecking.. I've been trying to get ajax to fire through a custom route, then backed down into trying to have a submit button but am getting an undefined methodto_key' for #` error.


I've flapped around with this problem for two weeks, pouring back over rails tutorial after tutorial and I'm still stumped.


I've come to the conclusion i've missed something vital about the way checkboxes communicate back with a controller or that i've been ham-fisted when setting up my models (sooo many migrations to make that schema...)


Can anyone recommend a good rails form_for/checkbox tutorial for updating attributes that might help me crack this problem - or perhaps even just the terminology that best applies to what i'm trying to do. All answers I can find seem geared to slightly different problems (like has_many_and_belongs_to), or just dives into the view code without giving me a glimpse at what my controller should be up to!


For the record here's what things look like at the moment.


I have a database schema that looks as follows:



create_table "field_records", force: true do |t|
t.string "label"
t.string "name"
t.boolean "whitelist"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "checked"
t.integer "object_record_id"
end

add_index "field_records", ["object_record_id"], name: "index_field_records_on_object_record_id"

create_table "object_records", force: true do |t|
t.string "label"
t.string "name"
t.boolean "whitelist"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "checked"
t.datetime "whitelisted_at"
end


and a model setup:



class ObjectRecords < ActiveRecord::Base
has_many :field_records, dependent: :destroy
accepts_nested_attributes_for :field_records
validates :name, uniqueness: { case_sensitive: false }
end

class FieldRecords < ActiveRecord::Base
belongs_to :object_record
end


Any suggestions that could break me out rut would be much appreciated!





Aucun commentaire:

Enregistrer un commentaire