Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting hstore in rails4, dynamic key/values

I'm playing around with Hstore for the first time in a rails4 app, and I am using javascript in a form to build out dynamic form fields for the hstore column (:schema)

In rails 4 I dont need to add any setter/getter method in my model, correct?

In my form i am building dynamic input fields and allowing the user to set the key/value pairs. Much like the Hstore Heroku Demo App

So basically my form would have inputs like

input name="app[schema][dynamic_key1]" value="whatever value"
input name="app[schema][dynamic_key2]" value="whatever value2"

In my App Controller:

def app_params
  params.require(:app).permit(:name, :title, :schema )
end

However, when i create a new App record, my schema hstore values are not saving. I saw some things about making the strong param for :schema => [] but that still does not work.

Since I do not know what these values will be, i cant setup store_accessors for these like I have seen in a lot of examples.

like image 533
Joel Grannas Avatar asked Sep 27 '13 15:09

Joel Grannas


2 Answers

found this here: http://guides.rubyonrails.org/action_controller_overview.html#more-examples

and in my controller I used:

def app_params
  params.require(:app).permit(:name, :title).tap do |whitelisted|
    whitelisted[:schema] = params[:app][:schema]
  end
end
like image 151
Joel Grannas Avatar answered Oct 31 '22 09:10

Joel Grannas


I think Rails must have simplified this in recent versions (current as of 5.2.3 at least)... and much cleaner/easier:

params.require(:parent).permit(:name, :whatever, data: {})

This will allow and store any/all attributes of data into an hstore field. An example of POSTing or PUTing a data nested attribute via HTML:

<input type="text" name="parent[data][your_super_custom_nested_data] />`

4th example down: https://guides.rubyonrails.org/action_controller_overview.html#more-examples

like image 44
Ryan Avatar answered Oct 31 '22 09:10

Ryan