select within loop in Rails 3, taking only first option - ruby-on-rails

Guys I have a form like this
<%= form_tag({:action => 'update_survey_list_status') do%>
<table id="survey_form">
<tr>
<th>Beneficiary Details</th>
<th>HFI details</th>
<th style="vertical-align: middle">
<input type='checkbox' name='checkall' onclick='checkedAll();'></th>
</tr>
<% #to_be_registred_list.each do |b| %>
<tr>
<td style="vertical-align: middle"><b> <%= b.name %></b><br />
<span><%= b.age %>, <%= b.sex %><br /><%= b.address %></span>
</td>
<td style="vertical-align: middle">
<%= select("beneficiaryloans","hfi_id",
Beneficiary.find(b.beneficiary_id).beneficiaryloans.collect
{
|h| [User.find(h.hfi_id).name+" - Rs. #{h.amount} # #{h.rate} % for
#{h.period}", h.hfi_id] }) %>
</td>
<td> <%= check_box_tag "benificiary_loan_ids[]", b.beneficiary_id,:name =>
"benificiary_loan_ids[]"%>
</td>
</tr>
<% end %>
</table>
<%end %>
Here, within a form, #to_be_registred_list will return 2 records, for each |b| record, within a select tag
Beneficiary.find(b.beneficiary_id).beneficiaryloans.collect will return many records. so each drop-down filled with associated values. here if I submits the form, even after selecting different records from each drop-down, it will only takes first option from each drop-down. how to solve this?
I need what I selected form each drop-down!

Did you used the debug on rails?
Use it and check the params you're receiving

Related

getting image name or path from file field control into controller's page

I have a file field control on my page like below:
<h2><%= #blah %>hello</h2>
<%= form_for(:ImageUpload, :html => { :id => "imageupload" }) do |f| %>
<table width="100%">
<tr>
<td align="left">
<%= file_field 'upload', 'datafile' %>
</td>
</tr>
</table>
<table width="92%">
<tr>
<td align="center">
<div class="button" style="margin-right:60px;">
<%= f.submit "Next", { :class => "buttonSearch"} %>
</div>
</td>
</tr>
</table>
<% end %>
And I want to use file field for getting image and set into user profile, for that I want to get image name or image path and i am using below:
def create
#blah = params[:upload]
upload_img = params[:upload]
render 'new'
end
But in the #blah variable, it gets me like this {"datafile"=>"index.jpg"}hello, How do I am getting image name or path from file field control into a controller's page, Kindly suggest me, waiting for your reply.
Thanks.
You might be missing something here
#blah = params[:upload]["datafile"]
Use gem carrierwave
Otherwise u need to do file operations to save the image get the images. Make your work simple.
See documentation here http://carrierwave.rubyforge.org/rdoc/
Please add in your form_for tag multipart: true option.
Syntax:-
<h2><%= #blah %>hello</h2>
<%= form_for(:ImageUpload, :html => { :id => "imageupload",:mulitipart => true}) do |f| %>
<table width="100%">
<tr>
<td align="left">
<%= file_field 'upload', 'datafile' %>
</td>
</tr>
</table>
<table width="92%">
<tr>
<td align="center">
<div class="button" style="margin-right:60px;">
<%= f.submit "Next", { :class => "buttonSearch"} %>
</div>
</td>
</tr>
</table>
<% end %>

rails dynamic selection from list

i need to change content depending which items is chosen. I am pretty new to rails and cant figure out how to do this. I know i need to use ajax, i guess! Okey.
Here is the list
<div class="table_wrap city_list_wrap">
<table class="city_list table normal">
<tbody>
<tr>
<th colspan="3"> City list </th>
</tr>
<% #cities.each_slice(3) do | slice | %>
<tr>
<% slice.each do |city| %>
<td class="city_tab"><%= city.title %> (<%= city.car_dealers_count %>) </td>
<% end %>
<tr>
<% end %>
</tbody>
</table>
</div>
And what after i push on one of the cities u need to show the list of that city items in table like this:
<div class="table_wrap">
<table class="table normal clickable" id="item_table">
<tbody>
<tr>
<th style="width:30%"><%= t("public.item.title") %></th>
<th style="width:45%"><%= t("public.item.info") %> </th>
<th style="width:25%"><%= t("public.item.logo") %> </th>
</tr>
<% city_title = "" %>
<% #items.each do | item | %>
<% unless city_title == item.city.title %>
<% city_title = item.city.title %>
<tr class="caption"><td colspan="3"><%= city_title %></td></tr>
<% end %>
<tr id="item-<%= item.id %>" class="hoverable">
<td><%= item.title %></td>
<td>
<p>
<%= item.address %><br><%= item.phone_number %><br/><%= link_to item.site, "http://#{item.site}", target:"_blank", rel: "nofollow", class: "item-link" if item.site.present? %>
</p>
</td>
<td><%= image_tag item.small_logo, alt: item.title %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
At the start this list is shown and all items are shown. After i click on one of the cities i need to show only items from that city in this second table.. How to do this? Please help. Thanks.
You can use this gem: https://github.com/innku/metropoli
Read the helper, it's real easy to install.
Okey i did this pretty simple way gave every city field id its id and rows with city items that id too. and after click $("id").hide() or show() depending on situation

Using partials, how do I render `striped` table rows in a DRY way?

So I have some view code that looks like this:
<div class="box-content">
<table class="table table-properties">
<tbody>
<%= render :partial => 'property', collection: #search.listings, as: :listing %>
</tbody>
</table>
</div>
In that _property.html.erb, I have this:
<tr>
<td class="span2">
<%= link_to listing_path(listing), :class => "thumbnail thumb2" do %>
<%= image_tag "room_1.jpg", :alt => "Lucas" %>
<% end %>
</td>
<td>
<h2><%= link_to listing.headline, listing_path(listing) %></h2>
<h5><%= listing.listing_type.name if listing.listing_type "#{listing.neighborhood.name.capitalize}" %></h5>
<h5>Maintenance <%= number_to_currency(listing.maintenance) %></h5>
</td>
<td class="span1">
<h2 class="price"><%= number_to_currency(listing.price)%></h2>
<h5><%= "#{listing.num_bedrooms} bed, #{listing.num_bathrooms} bath" %></h5>
</td>
</tr>
Basically, I want to generate that above code exactly, for each row, the only difference is I want every 2nd row (i.e. all even numbered rows) to have the class=striped..i.e. <tr class=striped>.
Thoughts on how to do this in a DRY way?
Have you tried using cycle and current_cycle ?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle
<tr class="<%= cycle('odd', 'even') -%>">
<!-- etc -->
</tr>
This alternates your classes with odd and even and IMHO should also work when render a collection. If you need the value of the actual cycle multiple times, you get it with current_cycle (because calling cycle multiple times would mess up your cycles, unless you want that).
Won't it be better just to use :nth-child() css selector? http://www.w3schools.com/cssref/sel_nth-child.asp
You can try something like this:
<tr class="<%=cycle("odd", "even") %>">
<td class="span2">
<%= link_to listing_path(listing), :class => "thumbnail thumb2" do %>
<%= image_tag "room_1.jpg", :alt => "Lucas" %>
<% end %>
</td>
<td>
<h2><%= link_to listing.headline, listing_path(listing) %></h2>
<h5><%= listing.listing_type.name if listing.listing_type "#{listing.neighborhood.name.capitalize}" %></h5>
<h5>Maintenance <%= number_to_currency(listing.maintenance) %></h5>
</td>
<td class="span1">
<h2 class="price"><%= number_to_currency(listing.price)%></h2>
<h5><%= "#{listing.num_bedrooms} bed, #{listing.num_bathrooms} bath" %></h5>
</td>
</tr>
Do it with jquery:
$(document).ready(function(){
$("table.table-properties > tbody > tr:odd").addClass("odd");
$("table.table-properties > tbody > tr:not(.odd)").addClass("even");
});

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.

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