Make Editable (In Place) Table Ruby On Rails - 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

Related

update database with checkboxes - 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.

Rails search table clickable link

I have a keyword search page for which the results are presented in a table. One of the fields in this table is a url which I want to display as a clickable link when the field has one in it but I can't get this working, when the link is clicked it instead queries the search again. I think the problem is due to the def within the searches controller dealing with the keywordsearch view. Can anybody help?
Below is the keywordsearch view which contains the results table:
<!-- Index of all Courses -->
<% provide(:title, "Courses Page") %>
<!--Breadcrumbs -->
<br>
<%= link_to "Back", :back %><br><br>
<!--Page Contents -->
<div class ="row">
<h1>Degrees Offered</h1>
<%= image_tag "line.png" , :alt => "line break"%>
</div>
<div class ="row">
<!-- Form for Keyword Search, to query database for University courses. It is hidden so as to not appear as a search on the page -->
<div class = "hidden">
<%= form_tag(keywordsearch_path, :method => "get", id: "search-data") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search course" %>
<%= submit_tag "Search" %>
<% end %>
</div>
<% if #search_degree != nil %>
<% end %>
<% if #search_degree != nil %>
<table border="1" class="table">
<thead>
<tr>
<th>University Name</th>
<th>Course Name</th>
<th>Duration</th>
<th>Qualification</th>
<th>Entry Requirements</th>
<th>Course Page</th>
</tr>
</thead>
<tbody>
<% #search_degree.each do |degree| %>
<tr>
<td><%= degree.uname %></td>
<td><%= degree.cname %></td>
<td><%= degree.duration %></td>
<td><%= degree.qualification %></td>
<td><%= degree.entry %></td>
<td> <a href=<% degree.url %>>View course page on University Website</a></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
Below is portion of the search controller in which keyword search is defined:
def keywordsearch
#search = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
#search_degree = Degree.search(params[:search]).order('uname ASC')
end
end
The link from all of this is clickable but rather than taking me to the correct url it redoes the search.
try this instead of hardcoding the <a> tag:
<%= link_to 'View course page on University Website', degree.url %>
Can you also post a sample of url? Be sure to include the protocol (e.g, http://...)

Accept Nested Attributes not working as expected

So I have an entity form that accepts nested attributes for another entity. This renders perfectly fine. However, the nested form is not functioning as I would like it to. See below:
<table class="datagrid">
<thead class="datagrid">
<th width="300" align="left"> Item</th>
<% unless current_user.is_partner? %>
<th width="100"> Location</th>
<% end %>
<th> Amount Requested</th>
<th> Amount Checked Out</th>
</thead>
<% session[:clicked_request].items.each do |item| %>
<tr class="<%= cycle('dg_list_line_odd', 'dg_list_line_even') %>">
<td> <%= link_to "#{item.name}", item_path(item) %></td>
<% unless current_user.is_partner? %>
<td> <%= item.location.name %></td>
<% end %>
<td> <%= item.requested %></td>
<td><%= f.text_field :amount, :value => item.requested, :size => 1 %></td>
</tr>
<% end %>
</table>
<p> </p>
As you can see there is an "each" loop here, that allows for the display, and hopefully the creation, of multiple items. However, when I press the submit button, regardless of how many items are present, only one of them is created.
Your suggestions are much appreciated.
That's not how you render forms with accepts_nested_attributes
Assuming f is a form builder bound to the parent object, you need to do something like
<%= f.fields_for :items do |item_form| %>
<%= item_form.text_field :amount
<% end %>
The block will be executed once for each item in the collection. In addition, item_form is setup with the right prefix for the controller side to work properly.

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