looping over columns in rails - sql

This is probably very easy, but I'm having a hard time figuring it out.
I have a partial:
<% for room in #scrape %>
<tr id="page_<%= room.id %>">
<th scope="row" class="<%= cycle("spec", "specalt") -%>"><%=h room.name %></td>
<td class="<%=current_cycle%>"><%=h room.day1 %></td>
<td class="<%=current_cycle%>"><%=h room.day2 %></td>
<td class="<%=current_cycle%>"><%=h room.day3 %></td>
<td class="<%=current_cycle%>"><%=h room.day4 %></td>
<td class="<%=current_cycle%>"><%=h room.day5 %></td>
<td class="<%=current_cycle%>"><%=h room.day6 %></td>
<td class="<%=current_cycle%>"><%=h room.day7 %></td>
<td class="<%=current_cycle%>"><%= select_tag("room[#{room.id}]", options_for_select(0..room.spots,0)) %></td>
</tr>
<% end %>
From a find_by_sql result like so:
ID Room Day1 Day2 Day3 Day4 Day5 Day6 Day7
18298 Blue Room 13.23 13.23 13.23 13.23 13.23 13.23 13.23
But I don't know how many days there will be, how can I loop thru the column results for the different Days?

This could be done in a helper using block/yield, but that's outside the scope of your question. I'll get right to the question by doing this inside the partial.
<% room.attributes.each do |key, value| %>
<% if key.to_s.include?("day") %>
<td class="<%=current_cycle%>"><%=h value.to_s %></td>
<% end %>
<% end %>
Update:
Here is the helper example. If this pattern is showing up more than once in your app, I think this is both more maintainable and readable.
def attributes_for(model, match, &block)
model.attributes.each do |key, value|
if key.to_s.include?(match)
# we pass key and value in this example. but you can
# pass whatever you want to the block.
concat(capture(key, value, &block))
end
end
end
And this is now your partial:
<% attributes_for(room, "day") do |key, value| %>
<td class="<%=current_cycle%>"><%=h value.to_s %></td>
<% end %>
More total lines of code, but better if you are going to be doing this throughout your app.

Related

Separation space between controls disappeared after setting bootstrap in my project

I am working on a Rails project and I just added bootstrap to take advantage of some styles. I confirmed that it is working correctly because I performed some tests styling a table and defining cols for the grid system for example. However, I have noticed an issue which I will explain based on the code below. As you can see, there is an HTML table with 2 rows to let a user type email and password. Before including bootstrap in my project, both text fields showed a separation space between them. After including bootstrap, there is no separation space. I am including an image so you can see how both text field appear one above the other without any spacing.
I will very much appreciate your feedback.
<div>
<%= form_tag(access_attempt_login_path, :method => :post) do %>
<table>
<tr>
<td><%= label_tag(:email) %></td>
<td><%= text_field_tag(:email, params[:email]) %></td>
</tr>
<tr>
<td><%= label_tag(:password) %></td>
<td><%= password_field_tag(:password) %></td>
</tr>
<tr>
<td> </td>
<td><%= submit_tag("Log In") %></td>
</tr>
</table>
<% end %>
</div>

Table historic with all the authorizations ids checked update column

I have this problem... I need save my table historic with all the authorizations ids checked, update the column authorization_origin_id and set the column refinancing_id with the refinancing id created. Example: I checked authorizations ids 2 and 3, so I create historic with two lines with authorization_origin_id 2 and 3 and the refinancing id will be 1. My code:
My view this it:
<%= simple_form_for(#refinancing) do |f| %>
<div class="form-inputs">
<%= f.hidden_field :employee_id, value: #employee.first.id %>
<%= f.hidden_field :authorization, value: #authorization %>
<%= f.input :contract_number %>
</div>
<h3>Reserved value</h3>
<table class="table table-condensed table-bordered table-striped">
<thead>
<th>Authorization id</th>
<th>Contract number</th>
</thead>
<% #authorizations.each do |authorization| %>
<tbody>
<tr>
<td><%= authorization.id %></td>
<td><%= authorization.contract_number %></td>
</tr>
</tbody>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit, "To Reserve" %>
</div>
<% end %>
Controller is this:
def new
if params[:authorization].present?
#selected_ids = params[:authorization][:contract_number]
#authorizations = Authorization.where("contract_number in (?)", #selected_ids)
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
#authorizations.ids.each do |auth_id|
#historic_refinancing = HistoricRefinancing.create
#historic_refinancing = HistoricRefinancing.update_all(authorization_origin_id: auth_id)
end
end
#employee = Employee.search_cpf(params[:search_employee_by_cpf])
#refinancing = Refinancing.new
end
Actually my table historic is wrong, authorization_origin_id take, just the last (just one ever =/) value checked and refinancing_id is null
The answer is it:
#historic_refinancing = HistoricRefinancing.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)
or simply this
#historic_refinancing = HistoricRefinancing.create(authorization_origin_id: auth_id)

Grouping records with headings in Rails 3

I have an articles model in my Rails 3 application. The article model has a column called categories which is set using a select box in the new form view. (It's a select box because the options should never change and there are only four of them).
The index view code I have is as follows:
<% #articles.category.each do |article| %>
<%= article.category %>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.author %></td>
<td><%= article.category %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
<% end %>
I have grouped by category in my controller:
#articles = Article.group(:category).order('title ASC')
However, this results in an exception which points to the following line <% #articles.category.each do |article| %>.
What is the tidiest (from within my view code) way of achieving the following:
Category 1
Article 1
Article 2
Category 2
Article 5
Category 3
Article 8
So each article is listed under it's category.
I'd suggest you to use group_by method (Documentation):
# in your controller
articles = Article.order('title ASC')
#grouped_articles = articles.group_by &:category
# in your view
<% #grouped_articles.each do |category, articles| %>
<%= category %>
<% articles.each do |a| %>
<tr>
<td><%= a.title %></td>
<td><%= a.author %></td>
<td><%= link_to 'Show', a %></td>
<td><%= link_to 'Destroy', a, confirm: 'Are you sure?', method: :delete %> </td>
</tr>
<% end %>
<% end %>

How to get selected value in select in rails 3?

I want to know how to get the selected value of <select>. I only know how to populate this.
Here's is my code in index.html.erb. I used this to populate the <select> dropdown menu.
<h1>Trap</h1>
<%= form_for #search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, #employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link #search, :empcode %> |
<%= sort_link #search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
And in my controller traps_controller.rb:
def index
#search = Trap.search(params[:search])
#traps = #search.all
#employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
Pls tell me how to get the selected value if the user selects a value. I don't have any idea.
Try (in the controller) :
value = params[:empcode_contains]
or you could fetch the whole object this way:
search = params[:search]

Rails 3 i need some help with making a FAQ page

first off ive tried google and cant seem to find an answer i can understand what i would like to do is list my questions from an active record witch i have that much working then make the question its self a link so her is my page so far!
<h1>Listing faqs</h1>
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<% end %>
</table>
<br />
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<tr><td valign="top"><div id="right">Answer : </div></td><td><%= faq.answer %></td></tr>
<% end %>
</table>
<br />
<%= link_to 'New Faq', new_faq_path %>
im amusingenter code here i can stick something in before faq.question like a link_to or something or something but im not shure what
First of all, in Rails 3 forms should start with <%=, not <%.
<%= #faqs.each do |faq| %>
<tr>
<td width="90px">
<div id="right">Question : </div>
</td>
<td>
<%= link_to(faq.question, faq_path(faq.id) %>
</td>
</tr>
<% end %>