3

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
11
  • You are doing fine, what is the issue? Commented Dec 1, 2016 at 4:34
  • @AnujDhanju i've update my question above Commented Dec 1, 2016 at 5:41
  • you can require 'json' and use to_json on the hash Commented Dec 1, 2016 at 5:48
  • @Sravan yup i did that in my controller.. but still produce =>.. see my updated above Commented Dec 1, 2016 at 5:49
  • check, puts { 'alert' => params[:message][:alert] }.to_json Commented Dec 1, 2016 at 6:02

2 Answers 2

2

You can use, to_json to convert the ruby hash to JSON object

just require 'json' in your controller.

params = {"utf8"=>"✓", "_method"=>"new", "authenticity_token"=>"txroAHF2+YZOrm48DtBZdVqKzxLYyHFq4+GWQFnM6kNldXgRZJMPv0yfj‌​0/tfZVpuVvh39UVX4Fb66FNkkCZqA==", "message"=>{"name"=>"Dan Murphy Roundabout Test", "company"=>"transtech", "location_id"=>"", "message_notification"=>[{"alert"=>"alert1"}, {"alert"=>"alert2"}], "commit"=>"save", "controller"=>"message", "action"=>"create", "id"=>"1717"}}

Since your params object is above, according to your requirement you can use,

params['message']['message_notification'] = (params['message']['message_notification'].to_json)

this converts the message_notification in the params to a json object, and stores in the DB.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not clear to me exactly where params['message']['message_notification'] = (params['message']['message_notification'].to_json) goes.
1

Here's what I did.

  def resource_params
    params.require(:appointment_modules_dataset_intake).permit(
      :appointment_id,
      :main_issue,
      :cvf
    ).tap do |p|
      p[:cvf] = JSON.parse(p[:cvf]) # This is the significant line
    end
  end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.