Search code examples
ruby-on-railsmodelcheckboxformhelper

Rails:How can I get these check_boxes and radio buttons to show the data from the model?


Here is some code that is used basically to show a table of listings from my database. Last few columns are nothing but radio buttons and check boxes that are tied to specific boolean attributes of the model. Ive programmed the check boxes and radio buttons. But How do I get them to display the values in the database?

index.html.erb:

<h1>Listings</h1>

<table class="datatable">
 <tr id="heading" >
  <th >id</th>
  <th >name</th>
  <th>telephone</th>
 </tr>

<% @listings.each do |listing| %>
  <tr id="body">
    <th><%=listing.id%></th>
    <th><%= link_to listing.name, edit_listing_path(listing) %></th>
    <th><%=listing.telephone%></th>

 <%= form_for listing do |f| %>     
 <td id="keep"><%= f.radio_button :keep, "Keep" %></br>
    <%= f.label :keep, "Keep" %>
 </td>
 <td id="delete"> <%= f.radio_button :keep, "Delete" %></br>
    <%= f.label :keep, "Delete" %>
 </td>
 <td id="checked"><%= f.check_box :checked %></br>
    <%= f.label :checked, "checked" %>
 </td>
 <td id="collected"><%= f.check_box :collected %></br>
    <%= f.label :collected, "collected" %>
 </td>
 <td id="digitized"><%= f.check_box digitized %></br>
    <%= f.label :digitized, "digitized" %>
 </td>
 <td id="in_db"><%= f.check_box :in_database  %></br>
    <%= f.label :database, "database" %>
 </td>
    <td id="submit"><%= f.submit "update" %></br>

  </td>
  <% end %>
   </tr>
 <% end %> 
 </table>

Also the reason Im choosing the "form" here is because eventually after I figure out how to show the data here, Id like to have this view also "update" the data in the model if the user changes any values in the checkboxes. But, Ill get to that once Ive learnt how to display the model's values.

Thanks


Solution

  • Something like this:

    <%= f.radio_button :keep, "Keep", :checked => listing.keep %>