update database with checkboxes - rails - ruby-on-rails

So I have a users model which is associated to activities and activities are associated to a completed model?.
What I am trying to do is update if an activity is completed when a checkbox is clicked.
Here is my view:
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
This all works great and shows if an activity is "finished" but I am having a hard time figuring out how to update the database with the checkbox. While I would like it to update when you click the checkbox I am ok with having a save button to save them all at once. Which ever would be easier probably would be best for me as I am just learning rails.
Thank you for any help.
Edit:
To clarify my question, my code above works for showing the data correctly, what I can't figure out is how to turn it into a form so that I can save any changes to the check box values.

The easiest solution would be to use a form and a save button. If you look at the Form Helpers Rails Guide, you can see an example:
<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
You probably want to use form_for, not form_tag though to update your object. See the documentation here.
<%= form_for task do |f| %>
<%= f.label :finished %>:
<%= f.check_box :finished %><br />
<%= f.submit %>
<% end %>
EDIT:
To update multiples, try something like this:
<% form_tag edit_multiple_products_path do %>
<table>
<tbody>
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Update Tasks" %>
<% end %>
Except don't forget to do everything else that the RailsCast mentions.

Related

Rails - Simple_fields_for not submitting multiple records

I'm trying to create a form that allows a user to select different choices for each category on the same form, and each of these will create its own model. My problem is getting the form to submit multiple parameters. My code is as follows:
<%= simple_form_for choices_path do |f| %>
<td><%= person.fullname %></td>
<td><%= person.email %></td>
<td><%= person.phone %></td>
<% if Category.any? %>
<%= f.simple_fields_for :choices do |g| %>
<% Category.all.each do |category| %>
<td>
<%= g.input :item_id, as: :select, collection: booking.venue.menu_items(category) %>
<%= g.hidden_field :admin_id, value: person.id %>
</td>
<% end %>
<% end %>
<% end %>
<td><%= person.completed? %></td>
<td><%= f.submit %></td>
<% end %>
The idea obviously being to create a new form for each category. This works until submitting the data, where only the last form's data is submitted. My paramaters are as follows:
{"utf8"=>"✓", "authenticity_token"=>"tf3DSLGHyOVEVJkcLjSVE9HDJbjn1CIaYBJIfHFw6RYH8tcn0tNilWVIjzyvcjXYm2ovKNO5A31+TktGA8X2+Q==",
"/choices"=>{"choices"=>{"item_id"=>"Dessert 1", "admin_id"=>"3"}},
"commit"=>"Save /choices",
"venue_id"=>"1",
"booking_id"=>"26"}
I can see that its creating a hash and is ready to submit multiple choices, however it is only the last record's data that is submitted.
Any help on how to get multiple records to update is greatly appreciated.
Thanks in advance.
The simple form association helper might be what you are looking for:
link

Make Editable (In Place) Table Ruby On Rails

I currently have a page that shows a quote (id, purpose of quote, etc) for a customer with a table also on the page that shows products a customer is wanting to buy that are in the quote.
I need to make the product table editable, and I do not want to use a gem because I would really like to know how this actually works.
So, here is what my page looks like so far:
<h1>Edit Quote</h1>
<%= form_for(#quote) do |q| %>
<p>
<%= q.label :service_tech %>
<%= q.text_field :service_tech %>
</p>
<p>
<%= q.label :purpose %>
<%= q.text_field :purpose %>
</p>
<p>
<%= q.submit %>
</p>
<% end %>
<p>Products</p>
<table>
<tr>
<th>Product/Part</th>
<th>Unit/Mdl</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
<% #products.each do |p| %>
<tr>
<td><%= p.product_part %></td>
<td><%= p.unit_mdl %></td>
<td><%= p.description %></td>
<td><%= p.quantity %></td>
<td><%= p.price %></td>
<td><%= p.total %></td>
<td>[<%= link_to "edit", edit_product_path(q) %>]</td>
</tr>
<% end %>
So, what I am wanting to do is remove my link to edit my product, and create the ability to edit the product in place and click a save button that saves the product.
I assumed that I would create some jquery function that when a save button is clicked the jquery grabs the new data from the table and passes the parameters to my update definition in my products controller. However, I do not know how to get the information from the table, make the table editable, and post the data I need to update the correct record.
Any help would be appreciated! Thanks

Rails show action where `id` is not explicitly stated in params

In my app, I have a view that shows a number of products. If a product has an active "deal" on it, a link to that deal is given for that deal that links to the show action for that deal, as below.
<tr>
<% #item.inventory_items.each do |p| %>
<td><%= link_to image_tag("#{p.vendor.image_url}"), vendor_path(p.vendor) %></td>
<td class="price">$<%= p.price %></td>
<td>
<% if Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id).exists? %>
<%= link_to "See It!", deal_path(Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id)) %>
<% else %>
<% end %>
</td>
<td>
......
</td>
</tr>
The problem I'm having is that my show action for Deal is looking in params for a deal id. This deal id isn't really in params, so how can I tell this Deal show action which deal I'm interested in? Thanks in advance! As an aside, my above code feels really verbose and very un-rails-like in my if block. If you have any suggestions for it, any advice is much appreciated.
deal/show.html.erb
<%= link_to image_tag("#{#deal.vendor.image}") %>
<%= #deal.vendor.address %>
<%= image_tag(#deal.image) %>
Deal show action:
def show
#deal = Deal.find(params[:id])
end
Try
<%= link_to "See It!", deal_path(Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id).first) %>
Your code is definitely not very rails-like.
What you should be able to do is something like this:
<tr>
<% #item.inventory_items.each do |p| %>
<td><%= link_to image_tag("#{p.vendor.image_url}"), vendor_path(p.vendor) %></td>
<td class="price">$<%= p.price %></td>
<td>
<% if p.deal.exists? %>
<%= link_to "See It!", deal_path(:id => p.deal.id)) %>
<% else %>
<% end %>
</td>
<td>
......
</td>
</tr>
Note that you can pass parameters along in a link_to. See this question - link_to send parameters along with the url and grab them on target page
If you're models are setup correctly, you shouldn't have to be doing queries in your views.

ruby-on-rails iterating through object attributes in view template aka .erb file

I'm using ruby on rails.
wondering if this is achievable.
Original Code
<%= form_for(:page, :url=>{:action => 'create'}) do |f| %>
<table summary="Subject Form Fields" %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<%end%>
desired code something along the lines of creating forms
by iterating through the object attributes.
<% for attribute in #subject.attributes.keys %>
<tr>
<td><%= attribute.humanize %></td>
<td><%= #subject.attributes[attribute].to_s %></td>
</tr>
<% end %>
so I am not sure if this is possible.
I believe what you are looking for is a .each loop:
<% #subject.attributes.each do |attribute| %>
<%= attribute.humanize %>
<% end %>
That will loop through each attribute of the #subject. If you also want to loop through the keys of each attribute, you need to add another nested loop:
<% #subject.attributes.each do |attribute| %>
<% attribute.keys.each do |key| %>
<%= attribute.humanize %> or <%= key %>
<% end %>
<% end %>
Hope that helps.

fields_for with association question

How do I use association data from within a fields_for?
I've got a has many through relationship between users, schools and schools_users. A user can have multiple schools and a school has multiple users. In schools_users there is a user_role field in addition to the common user_id and school_id.
In the show view I have:
<% #school.schools_users.each do |user| %>
<tr><td><%= user.user.first_name %> <%= user.user.last_name %></td><td><%= user.user_role %></td></tr>
<% end %>
but I can't figure out how to do the same thing from within the fields_for on the edit page.
<%= f.fields_for :schools_users do |f| %>
<tr>
<td><%= NEED USER NAME HERE %></td>
<td><%= f.select(:user_role, active_role_options)%></td>
<td><%= link_to_remove_fields 'Delete', f %></td>
</tr>
<% end %>
Thanks for any help!
Firstly: You're assigning the block variable of your fields_for to f, which is the same f your form_for uses. You should use a different name, like user_fields.
With that:
<%= f.fields_for :schools_users do |user_fields| %>
<tr>
<td><%= user_fields.text_field :first_name %></td>
...
<% end %>

Resources