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

Nanda Kishore

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?

Thuy Nguyen

You can remove option :checked => true, try

<%= check_box_tag '1', true, @present_user.misc_data[:pin_modules].include?(1), data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %>

and replace others follow this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i make my variables not retain their value after executing?

How to retain values from textarea after submitting the form

How do I redirect users after successfully submitting form data?

Updating MySQL table after submitting form with checkboxes

Retain check boxes checked whenever i hit back button after submitting the form

How do I create a form with checkboxes in slim?

How can I decrement a field value from firestore after submitting a form successfully?

How do I get errors which are handled serverside in a ajax error message after submitting a form?

How to make a variable retain its value after reloading, in PHP?

How to make pseudo class :invalid apply to an input AFTER submitting a form

How do I make rails form responsive to a hyperlink?

How do I post form data without submitting the form with jQuery?

How do I stop the form submitting with invalid fields in a form?

How can I retain checkboxes checked state after page submit and reload?

How do I make a form appear after clicking a button?

how to instantly load a list of options after submitting a value in a django form

How to compare value of the form before and after submitting in Symfony?

How can I empty this AngularJS form after submitting the data?

How can I show the output vertically after submitting the html form?

How can I get the form values to appear in the table after submitting?

How to redirect after submitting form

How do I retain selected option after submit?

How can I use javascript to add a value to a form before submitting it?

Make hidden elements visible after submitting form

How do I validate float values so that nothing but a float can be saved before submitting a form in ruby on rails 4?

How do I retain the value of a for loop variable when the loop is over?

How do I stop the form submitting until the end?

how do i stop users from submitting the same form on react

Symfony 3: set value after submitting a form