jeudi 1 décembre 2016

RUBY JSON save data only when own checkbox is checked

I have this form:

<div class='first'>

  <div>
    <%= check_box_tag "ng_geofence[driver_notification][][trigger_event]", 'IOR', false, class: 'ior' %>
    <%= label_tag "ng_geofence[driver_notification][][trigger_event]", 'Ignition within a Geofence' %>
  </div>

  <div id="div-first">
    <div class='fleft'>
      <%= label_tag "ng_geofence[driver_notification][][ptc][id]" do %>
        Pre-Trip Checklist
        <%= select_tag "ng_geofence[driver_notification][][ptc][id]", options_from_collection_for_select(@pretrips, "id", "name"), prompt: '-- choose --', wrapper: false, label: false %>
      <% end %>
    </div>

    Please enter a message to notify driver:
    <%= text_field_tag "ng_geofence[driver_notification][][message]", '', maxlength: 256 %>
  </div>

</div>


<div class='second'>

  <div>
    <%= check_box_tag "ng_geofence[driver_notification][][trigger_event]", 'GEN', false, class: 'gen' %>
    <%= label_tag "ng_geofence[driver_notification][][trigger_event]", 'Geofence' %>
  </div>

  <div id="div-second">
    <div class='fleft'>
      <%= label_tag "ng_geofence[driver_notification][][ptc][id]" do %>
        Pre-Trip Checklist
        <%= select_tag "ng_geofence[driver_notification][][ptc][id]", options_from_collection_for_select(@pretrips, "id", "name"), prompt: '-- choose --', wrapper: false, label: false %>
      <% end %>
    </div>

    Please enter a message to notify driver:
    <%= text_field_tag "ng_geofence[driver_notification][][message]", '', maxlength: 256 %>

    Remind Interval:
    <%= text_field_tag "ng_geofence[driver_notification][][remind_interval]" %>
  </div>

</div>

and here is my controller:

def create

  params['ng_geofence']['driver_notification'] = (params['ng_geofence']['driver_notification'].to_json)
  @geofence.driver_notification = (params['ng_geofence']['driver_notification'].to_json)
  @geofence.driver_notification

  respond_to do |format|
    if @geofence.update_attributes(geofence_params)
      format.html { redirect_to geofence_path(@geofence), notice: 'Geofence was successfully updated.' }
      format.json { head :ok }
    else
      format.html { render action: 'new' }
      format.json { render json: @geofence.errors, status: :unprocessable_entity }
    end
  end

end

And the result will save in table (column) :

[
  {
    "trigger_event":"IOR",
    "ptc":{ "id":"" },
    "message":"hello world"
  },
  {
    "trigger_event":"IOR",
    "ptc":{ "id":"" },
    "message":"",
    "remind_interval":""
  }
]

which is wrong.

Question is how to make, when the first checkbox is checked, only id="div-first" will save. same thing when the second checkbox is checked. Like below :

If first checkbox is checked:

[
  {
    "trigger_event":"IOR",
    "ptc":{ "id":"" },
    "message":"hello world"
  }
]




Aucun commentaire:

Enregistrer un commentaire