jeudi 26 décembre 2019

How Do I make Checkboxes retain their value after submitting the form in Rails

This Is the form

<%= form_tag method: "post" do |f| %>
 <script src="https://cdn.iris.nitk.ac.in/design_system/assets/js/vendors/form-components/toggle-switch.js"></script>

<table class="table table-striped">
<thead>
<tr>
  <th></th>
  <th>Title</th>
  <th>Pin To Modules Tab?</th>


</tr>
</thead>

<tbody>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/button_Human.png", class: 'list_image') %></td>
  <td>Profile</td>
  <td><%= check_box_tag '1', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/withdraw.png", class: 'list_image') %></td>
  <td>Withdraw Admission</td>
  <td><%= check_box_tag '2', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/attendance.png", class: 'list_image') %></td>
  <td>Feedback Forms</td>
  <td><%= check_box_tag '4', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <% if can? :my_grade_card, Student %>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/button_manage_users.png", class: 'list_image') %></td>
  <td>My Grade Card</td>
  <td><%= check_box_tag '5', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
  <% end %>
</tr>
</tbody>
</table>


<%= submit_tag "Pin Modules",:class => "btn btn-primary btn-lg btn-block" %>

This is how i'm saving in my db

def pin_modules

if request.post?
  present_user = User.find(current_user.id)
  present_user.misc_data[:pin_modules]=[]
    (1..17).each do |i|
      (params[i.to_s] ? (present_user.misc_data[:pin_modules] ||= []) << i : next)
    end
  present_user.save!
  redirect_to '/'
end

end

if the value is true then its saved in my db. and for some reason as i have seen the form params values which are only true are passed. i want my form to retain its value either if its true or false for a particular user. can someone help me with this?

Aucun commentaire:

Enregistrer un commentaire