Accept Nested Attributes not working as expected - ruby-on-rails

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.

Related

Rails 5 ActiveRecord Update Serialized Array in Form

I have a field that is a serialized array. It's loaded into my model and accessed in a form:
class Site < ApplicationRecord
serialize :steps, Array
end
<table class="listing" summary="Site list">
<tr class="header">
<th>Name</th>
<th>Step 1</th>
<th>Step 2</th>
<th>Step 3</th>
<th>Actions</th>
</tr>
<% #sites.each do |site| %>
<tr>
<td><%= site.name %></td>
<% site.steps.each do |step| %>
<td><%= step %></td>
<% end %>
<td class="actions">
<%= link_to("Show", site_path(site), :class => 'action show') %>
<%= link_to("Edit", edit_site_path(site), :class => 'action edit') %>
<%= link_to("Delete", delete_site_path(site), :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
I'm trying to update my edit form so that I can edit each "step" in the array.
<%= form_for(#site) do |f| %>
<table summary="Site form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<% a=1 %>
<% #site.steps.each do |step| %>
<tr>
<th>Step <%= a %>
<td><%= text_field :site, :steps, :value => step %></td>
<% a += 1 %>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Site") %>
</div>
<% end %>
The edit form displays the steps field correctly as each individual string in the array. However, when I attempt to submit the form I get the following error:
Attribute was supposed to be a Array, but was a String.
So steps is being submitted as the last entry in the array. How can I display the form correctly and also present the updated array back to the controller for processing?
Your text_field would need to multiple set to true. I believe in your case, something like this should work.
<%= f.text_field(:steps, { multiple: true, value: #site.steps[step] }) %>

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://...)

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

How can I edit a single field for an array of object?

I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>

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.

Resources