1

I have a cgi_attributes hstore type column in the UrlCommand model.

class UrlCommand < ActiveRecord::Base
    store_accessor :cgi_attributes, :name, :range, :security, :default_value
end

However, the keys in cgi_attributes should be dynamically added by users.

And I also want to render each key as a input filed in my form

Rather than hard-code the columns

  - [:name, :range, :security].each do | column |
    = render :partial => 'attributes' , :locals => { f: f, column: column }

And also need to add those dynamically generated key can be update into my Model.

def url_command_params
  params.require(:url_command).permit(:model_name, :firmware_version, :cgi_attributes,
                                       :cgi_name,:name, :range, :security)

end

Now, All of my code are based on hard-code, How to make the keys and value can be dynamically added by users and store into the UrlCommand model ?

newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

0

Maybe you can create a separate table for the attribute names (CustomAttributes) and then create a loop that adds them with store_accessor like this:

CustomAttributes.all.each do |attribute|
     store_accessor :cgi_attributes attribute.name
end

I guess you can do something similar to strong parameters. I have not tried this my self, because I'm struggeling to get hstore working in my application. But i will have the same user situation/ problem to solve. So i will test it and change my answer once i get it working and confirmed.

a bit like it is explained in this railscast: http://railscasts.com/episodes/403-dynamic-forms

Ole Henrik Skogstrøm
  • 6,353
  • 10
  • 57
  • 89