Rails form processing - ruby-on-rails

I have a form with a store number (collection select box), date (3 select boxes), and a list of items with a quantity field beside each item. I have been able to pass the items and quantities as an array of hashes, but I can't get the date and store numbers in because I can't seem to change their name to include the [].
Here's the code
<% form_for(#credit) do |f| %>
<% stores = Store.find(:all, :conditions => { :active => true }, :order => :storeNum) %>
Store Number<%= f.collection_select :storeNum, stores, :storeNum, :storeNum %>
<br /><%= f.date_select :credDate %>
<table>
<tr>
<th>Quanity</th>
<th>Item Number</th>
<th>Name</th>
</tr>
<% #items.each do |item| %>
<tr>
<td>
<%= f.text_field ( :quantity, :name => "credit[][quantity]", :size => 3 ) %>
<%= f.hidden_field :item, :name => "credit[][itemNum]", :value => item.itemNum %>
</td>
<td><%=h item.itemNum %></td>
<td><%=h item.name %></td>
</tr>
<% end %>
<tr><td colspan="3">
<%= f.submit 'Submit' %></td>
</tr>
</table>
<% end %>
Any help is surely appreciated. I've been working on this two days.

Have you considered simple HTML inputs?
<input type="text" name="credit[][quantity]" size="3" id="quantity"></input>

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] }) %>

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 %>

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.

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
Something like this:
<%= f.radio_button :keep, "Keep", :checked => listing.keep %>

Updating multiple tables in one form in Rails 2.3.x

Seems there should be a super-quick Railsy way to do this, but excessive Googling has yielded nothing to clear things up.
OK. I need to update three tables from one form. The view part of the equation is well-documented (form_for, fields_for). But I'm unsure how to handle the form params that are sent to the update action in the parent controller. Note: The three tables all have a defined Rails association.
View:
<% form_for #account do |a| %>
<table>
<tr class="odd">
<td>
<%= a.label :plan_id %>
</td>
<td>
<%= a.text_field :plan_id %>
</td>
</tr>
<tr class="even">
<td>
<%= a.label :company %>
</td>
<td>
<%= a.text_field :company %>
</td>
</tr>
<% fields_for #user do |u| %>
<tr class="odd">
<td>
<%= u.label :first_name %>
</td>
<td>
<%= u.text_field :fname %>
</td>
</tr>
<tr class="even">
<td>
<%= u.label :last_name %>
</td>
<td>
<%= u.text_field :lname %>
</td>
</tr>
<tr class="odd">
<td>
<%= u.label :email %>
</td>
<td>
<%= u.text_field :email %>
</td>
</tr>
<tr class="even">
<td>
<%= u.label :phone %>
</td>
<td>
<%= u.text_field :phone %>
</td>
</tr>
<tr class="odd">
<td>
<%= u.label :title %>
</td>
<td>
<%= u.text_field :title %>
</td>
</tr>
<% end %>
<% fields_for #site do |s| %>
<tr class="even">
<td>
<%= s.label :domain %>
</td>
<td>
<%= s.text_field :domain_name %>
</td>
</tr>
<tr class="odd">
<td>
<%= s.label :url %>
</td>
<td>
<%= s.text_field :url %>
</td>
</tr>
<% end %>
<tr>
<td colspan="2">
<%= a.submit :submit %>
</td>
</tr>
</table>
<% end %>
Update action of accounts controller (This doesn't work):
account = params[:account]
user = account
site = account
account.save!
user.save!
site.save!
How do I handle the params that are posted to the update action? It seems straightforward to create new records in three tables from one form, but I don't know how to update those same three tables from the same form.
If accounts/users/sites are all related then your associations in your model will handle the "tables" for you. You should be thinking about your problem in terms of models (ie: your domain/business problem). Let's say that one user can have multiple accounts (otherwise, no point in the account model) and each account can own a site (web hosting management app?).
If you set up your associations in app/models/*.rb with the has_many etc relationships, the saving a form in the controller method (or in IRB) is pretty easy. The controller code you have is not right at all. You can't assign variables like that and expect Rails to do quite that much magic.
account.user = params[:user]
account.site = params[:site]
For nested forms. If you can't get that to work, use rails c:
account = Account.new
user = User.new
site = Site.new
account.user = user
account.site = site
account.save # will fail if you have constraints/validations
Get it to work in rails c and IRB and then look at your form and HTTP activity using Firebug or other browser tool. Put this in your controller method puts params and you'll see how nested forms post.

Resources