I have 1 column called 'message_notification' inside table 'configuration'. I want to produce save result as JSON object in this column:
message_notification: [
{
"alert" : "how are you doing today?"
},
{
"alert" : "where have you been today?"
}
]
for the form, i use
<%= simple_form_for(@configuration) do |f| %>
Alert 1: <%= check_box_tag "configuration[message_notification][][alert]", 'how are you doing today?' %><label>Alert 1</label><br/>
Alert 2: <%= check_box_tag "configuration[message_notification][][alert]", 'where have you been today?' %><label>Alert 2</label><br/>
<%= f.submit %>
<% end %>
How to achieve this?
[UPDATED]
above code will resolve as a ruby hash (not as JSON)
my controller
@configuration.message_notification = {
alert: params[:message][:alert]
}.to_json
result:
message_notification: [
{
"alert" => "how are you doing today?"
},
{
"alert" => "where have you been today?"
}
]
[UPDATED 2]
In console:
=> a = value.message_notification
=> "[{\"alert\"=>\"alert1\"}, {\"alert\"=>\"alert2\"}]"
=> puts a
=> [{"alert"=>"alert1"}, {"alert"=>"alert2"}]
=> nil
require 'json'and useto_jsonon thehash=>.. see my updated aboveputs { 'alert' => params[:message][:alert] }.to_json