Rails form associations - ruby-on-rails-3

I currently have a form in my application that uses select form helpers. It accesses 2 variables:
#user = ['JM', 'JH', 'RY']
#name = ['JASON', 'JOHN', 'RYAN']
My form looks like:
<%= form_for(:event, :url => {:action => :update, :id => #event.id}) do |e| %>
<table>
<tr>
<th>Initails</th>
<td><%= e.select(:initials, (#user)) %></td>
<th>Priroty</th>
<td><%= e.select(:priroty, (1..10)) %></td>
</tr>
<tr>
<th>Name</th>
<td><%= e.select(:name, (#name)) %></td>
<th>Event</th>
<td><%= e.text_field(:event_name) %></td>
</tr>
<tr>
<th>Deadline:</th>
<td><%= e.datetime_select(:deadline, :order => [ :month, :day]) %></td>
<th>Complete ?</th>
<td><%= e.check_box(:complete) %></td>
</tr>
<tr>
<th>Event Description</th>
<td><%= e.text_area(:event_description, :size => '80x10') %></td>
</tr>
<tr>
<th>Comments</th>
<td><%= e.text_area(:comment, :size => '80x10') %></td>
</tr>
</table>
<table>
<div class="actions">
<%= e.submit %>
</div>
</table>
<% end %>
When I select my initials and it shows #user, I am trying to populate the name field that uses #name. Does anybody know how to associate the 2 so when I select JM for instance it pulls up Jason in the name field?

There is no way to do it without javascript.

Take a look at observe_field
And this tutorial

Related

How to create Profile user page using asp.net mvc 4 with sql server 2008?

I want to ask about create profile user page using asp.net mvc 4 and sql server 2008. Before i ask about it. I have create source code in controller like this:
namespace SIDLubricants.Controllers
{
public class ProfileController : Controller
{
private Tugas_AkhirEntities db = new Tugas_AkhirEntities();
//
// GET: /Profile/
public ActionResult Index(LoginModel model)
{
var m_cp_customer = from cp in db.M_cp_Customer.Include(m => m.User) where cp.Id == model.Id select cp;
return View(m_cp_customer.ToList());
and i have create view like this :
<div id="content" class="span12">
<div class="row-fluid sortable">
<div class="box span12">
<div class="box-header well">
<h2>Contact Person Profile</h2>
<table>
<% using (Html.BeginForm()) { %>
<%: Html.AntiForgeryToken() %>
<%: Html.ValidationSummary(true) %>
<tr>
<td><%: Html.DisplayName("Contact Person Customer") %></td>
<td><%: Html.DisplayFor(model => model.kd_cp_customer) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("First Name") %></td>
<td><%: Html.DisplayFor(model => model.NamaDepan) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Last Name") %></td>
<td><%: Html.DisplayFor(model => model.NamaBelakang) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Birth Date") %></td>
<td><%: Html.DisplayFor(model => model.Tgl_Lahir) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Gender") %></td>
<td><%: Html.DisplayFor(model => model.JenisKelamin) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Address") %></td>
<td><%: Html.DisplayFor(model => model.Alamat) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("City") %></td>
<td><%: Html.DisplayFor(model => model.Kota) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Province") %></td>
<td><%: Html.DisplayFor(model => model.Provinsi) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Zip Code") %></td>
<td><%: Html.DisplayFor(model => model.KodePos) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Phone") %></td>
<td><%: Html.DisplayFor(model => model.Telp) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Mobile") %></td>
<td><%: Html.DisplayFor(model => model.Mobile) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("Email") %></td>
<td><%: Html.DisplayFor(model => model.Email) %></td>
</tr>
<tr>
<td><%: Html.DisplayName("UserName") %></td>
<td><%: Html.DisplayFor(model => model.User.userName) %></td>
</tr>
<tr>
<td><input type="button" onclick="" value="edit" /></td>
<td> <input type="submit" value="Delete" /></td>
</tr>
<% } %>
</table>
</div>
</div><!--/span-->
</div><!--/row-->
but, i got an error message like this:
Server Error in '/' Application.
--------------------------------------------------------------------------------
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SIDLubricants.Models.M_cp_Customer]', but this dictionary requires a model item of type 'SIDLubricants.Models.M_cp_Customer'
Can you help me about it. I want final output is show all of data about profile user after action log in. Thank you...
The problem is not SQL Server 2008, the problem is that you're returning zero or more entities that match the user's id. You need to add FirstOrDefault() to your query so you're not returning a IEnumerable<M_cp_Customer> but a single M_cp_Customer entity.
public ActionResult Index(LoginModel model)
{
M_cp_Customer m_cp_customer = (from cp in db.M_cp_Customer.Include(m => m.User)
where cp.Id == model.Id
select cp).FirstOrDefault();
return View(m_cp_customer);
}

Rails - how do I create a hyperlink to a page for a single record?

How do you make <%= post.name %> a link to its own page?
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
</tr>
<% end %>
similar what this code does, but without the string 'Show'
<td><%= link_to 'Show', post %></td>
what i always had in mind is like how php does it, you wrap it in
<a href="<?php phpcode() ?>">
how can i do similar result in ruby?
How about
<%= link_to post.name, post_path(post) %>
or even easier:
<%= link_to post.name, post %>

Rails 3 - How to Dynamically Duplicate Record and Submit Multiple Records on Single Form

I have an INVOICE form. To eliminate tons of typing, users need to enter invoice attributes, then click a button to copy the first record, change a couple attributes, then save all the records at once. Excel style. Help?
<% form_for #invoice, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table class="ExcelTable2007">
<tr>
<th class="heading">PO #<font color="red"> *</font></th>
<th class="heading">Invoice #</th>
<th class="heading">"Bill To" is Correct</th>
<th class="heading">Invoice Date</th>
<th class="heading">Date Received</th>
<th class="heading">AP Clerk Note</th>
<th class="heading">Division</th>
<th class="heading">GR Approver</th>
<th class="heading">GR Alternate Approver</th>
<th class="heading">Invoice Attachment</th>
</tr>
<tr>
<td class="heading"><%= f.text_field :po_number, :size => 10 %></td>
<td class="heading"><%= f.text_field :invoice_number, :size => 10 %></td>
<td class="heading"><%= f.check_box :bill_to_address %></td>
<td class="heading"><%= f.calendar_date_select "invoice_date", :time => false, :size => 12 %></td>
<td class="heading"><%= f.calendar_date_select "date_received", :time => false, :size => 12 %></td>
<td class="heading"><%= f.text_field :clerk_note %></td>
<td class="heading"><%= f.collection_select :division_id, Division.active.order(:division), :id, :division, :include_blank => true %></td>
<td class="heading"><%= f.collection_select :approver_username, Aduser.order(:last_name, :first_name), :username, :fullname, :include_blank => true %></td>
<td class="heading"><%= f.collection_select :alternate_approver_username, Aduser.order(:last_name, :first_name), :username, :fullname, :include_blank => true %></td>
<td class="heading"><%= f.file_field :attachment %></td>
</tr>
</p>
</table>
    <%= link_to "Add Invoice", clone_invoice_url(#invoice) %>
<br>
<br>
<div class="actions">
<%= f.submit "Submit" %>
</div>
</p>
<% end %>
I tried the following, but receive an invalid authenticity token:
def clone_invoice
#invoice = Invoice.find(params[:id]).dup
#invoice.save
end
Can anyone help me? Thanks in advance!
Try this instead.
def clone_invoice
#invoice = Invoice.find(params[:id])
#new_invoice = #invoice.dup
end
If your invoice has an attachment then you would also want to copy that.
dupfile(#invoice.attachement.to_s, #new_invoice.attachment.to_s)
In your routes file, try
match 'invoices/:id/invoice_clone' => "invoices#clone_invoice", :as => "invoice_clone"
Then in your link_to, change the clone_invoice_url to invoice_clone_path

Loop and generate table cells

I have a table that looks like the following:
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
</tbody>
</table>
Which outputs the following:
I am trying to generate blank rows that contain empty cells. I cannot seem to identify where I am going wrong. What is the best possible solution
Updated version
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
You're ending your loop too soon and you'll need a td for each th so change:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
To:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>

Rails 3 View - Rounding Up

I have a Rails view to display a grid by PERIOD and GENERAL LEDGER, and I want to display a cumulative total. In the case below, however, my #cumulative_total in the "TOTAL" column is rounding up to the nearest dollar, so no cents are shown, even though they are displayed correctly in the general_ledger columns. Any ideas what I'm doing wrong?
<% #cumulative_total = 0 %>
<div id="gl_crosstab">
<table>
<tr>
<th>Period</th>
<% #general_ledgers.each do |g| %>
<th><%= g.general_ledger_number %></th>
<% end %>
<th>Total</th>
<th>% Expended</th>
</tr>
<% #expected_billings.group_by(&:period_id).each do |eb| %>
<tr>
<td><%= eb[1].first.period.pe_number %></td>
<% eb[1].each do|p| %>
<td><%= number_to_currency(p.expected_amount) %></td>
<% end %>
<td>
<% #cumulative_total = #cumulative_total + eb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i} %>
<%= number_to_currency( #cumulative_total ) %> </td>
<td><%= number_to_currency((#cumulative_total/#sla.project_total)*100) %> % </td>
</tr>
<% end %>
<tr>
<td><b>Total Budget</td>
<% #total_expected_billings.each do |teb| %>
<td><b><%= number_to_currency(teb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %></td>
<% end %>
<td><b><%= number_to_currency(#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %> </td>
<td><b><%= number_to_currency((#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}/#sla.project_total)*100) %> % </b></td>
</tr>
</table>
</div>
I think that is because you are converting the billing amount to 'integers' while displaying:
#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i
If you change
billing.expected_amount.to_i
to
billing.expected_amount.to_f
it should work.