How to Correctly render partial template in Rails? - ruby-on-rails

I have a Listing model that I am using to represent a listing for a sublet (apartment), and I created a Filter model as a way for a user to filter listings to his liking. Here in my Filter class, that has a method listings to query listings based on a form submission.
class Filter < ActiveRecord::Base
attr_accessible :air_conditioning, :available_rooms, :bathrooms, :furnished, :negotiable, :new, :parking, :maximum_price, :private_bathroom, :show, :term, :total_rooms, :utilities, :washer_dryer
serialize :term
def listings
#listings ||=find_listings
end
private
def find_listings
listings=Listing.order(:price)
listings=listings.where("listings.price <= ?", maximum_price) if maximum_price.present?
listings=listings.where(total_rooms: total_rooms) if total_rooms.present?
listings=listings.where(available_rooms: available_rooms) if available_rooms.present?
listings=listings.where(bathrooms: bathrooms) if bathrooms.present?
listings=listings.where(term: term)
listings=listings.where(furnished: furnished)
listings=listings.where(negotiable: negotiable)
listings=listings.where(utilities: utilities)
listings=listings.where(air_conditioning: air_conditioning)
listings=listings.where(parking: parking)
listings=listings.where(washer_dryer: washer_dryer)
listings=listings.where(private_bathroom: private_bathroom)
listings
end
end
In order to show these, I created a show method for filter that I want to render a partial template. This is what I currently have, but it won't render the template I created called _listings.html.erb in /listings
<p id="notice"><%= notice %></p>
<%= #filter.listings.size %>
<%= render #filter.listings %>
And here is the template
<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>
<table id="listingTable" class="table table-bordered table-hover">
<tr>
<th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
<th>Address</th>
<th><u><%= "Price Per Month" %></u></th>
<th>Description</th>
</tr>
<% if #listings !=nil %>
<% #listings.each do |listing| %>
<tr onmouseover="this.style.cursor='pointer';"
onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
<td><%= image_tag listing.photo.url(:small) %></td>
<td><%= listing.address %></td>
<td>$<%= listing.price %></td>
<td width="40%"><%= listing.description %></td>
</tr>
<% end %>
<% end %>
<% else if #listings==nil %>
<p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>
</table>
I think my naming conventions are messed up, but I can't find the correct way to do this. Anyone have any suggestions. Also, the filter always seems to come up empty. I've tested it many times with simple filters but it never works. If anyone sees an error in my filter feel free to point it out. Thanks!

If you call render and pass it an array or relation, it will actually call the singular version on the partial. When you call
<%= render #filter.listings %>
What it ends up doing is equivalent the following:
<%= render :partial => 'listings/listing', :collection => #filter.listings %>
which is equivalent to
<% #filter.listings.each do |listing| %>
<%= render listing %>
<% end %>
Also, there is an error in or partial around the `<% end %> <% else if ... %>, it should be the following
<% if #listings != nil %>
<% #listings.each do |listing| %>
<tr onmouseover="this.style.cursor='pointer';"
onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
<td><%= image_tag listing.photo.url(:small) %></td>
<td><%= listing.address %></td>
<td>$<%= listing.price %></td>
<td width="40%"><%= listing.description %></td>
</tr>
<% end %>
<% else %> (since you already checked if it was nil, you it is implied at this point the value is nil
<p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>
I would recommend writing it the following way, as it is more Ruby-esque
<% if #listings.blank? %>
<p> Sorry, No Sublets Fit Your Criteria! </p>
<% else %>
<% #listings.each do |listing| %>
<tr onmouseover="this.style.cursor='pointer';"
onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
<td><%= image_tag listing.photo.url(:small) %></td>
<td><%= listing.address %></td>
<td>$<%= listing.price %></td>
<td width="40%"><%= listing.description %></td>
</tr>
<% end %>
<% end %>
The way I would ultimately handle this case is the following:
Your Filter show view
<p id="notice"><%= notice %></p>
<%= #filter.listings.size %>
<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>
<table id="listingTable" class="table table-bordered table-hover">
<tr>
<th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
<th>Address</th>
<th><u><%= "Price Per Month" %></u></th>
<th>Description</th>
</tr>
<%= render(#filter.listings) || "<p> Sorry, No Sublets Fit Your Criteria! </p>".html_safe %>
</table>
Your 'listings/_listing.html.erb'
<tr onmouseover="this.style.cursor='pointer';"
onclick="window.location.href = '<%= url_for(listing) %>' " >
<td><%= image_tag listing.photo.url(:small) %></td>
<td><%= listing.address %></td>
<td>$<%= listing.price %></td>
<td width="40%"><%= listing.description %></td>
</tr>

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 5 edit multiple not showing values in table

I am creating multiple edit on my app. I have followed the revised Rails screencast and this code that adapts it to Rails 5.
In my Categories Controller:
def edit_multiple
#categories = Category.where(ID: params[:category_ids])
redirect_to categories_url
end
In my index:
<table class="table" summary="Category list">
<tr class="header">
<th style="text-align: left"><%= sort_link(#q, :NAME, 'NAME', default_order: :desc) %></th>
<th style="text-align: left">POSITION</th>
<th style="text-align: left">ACTIONS</th>
</tr>
<%= render "list" %>
</table>
<%= submit_tag "Edit Checked" %>
In my list partial:
<% form_tag edit_multiple_categories_path, method: :get do %>
<% #categories.each do |category| %>
<tr class="<%= cycle('odd', 'even') %>">
<td><%= check_box_tag "category_ids[]", category.ID %></td>
<td><%= link_to category.NAME, {:action => 'show', :ID => category.ID} %>
<td><%= category.RANK_POSITION %></td>
<td class="actions">
<%= link_to("Edit", {:action => 'edit', :ID => category.ID}, :class => 'button tiny') %>
<%= link_to("Delete", {:action => 'delete', :ID => category.ID}, :class => 'alert button tiny') %>
</td>
</tr>
<% end %>
<% end %>
When I open the categories page, it doesn't show anything from _list partial. There's only table headers and Edit Checked button.
Before adding the code relative to multiple edits the table was showing values from partial.
Note that db column names are capital as I'm building the app on existing db.
How can I fix it?
The problem was simply that
<% form_tag edit_multiple_categories_path, method: :get do %>
should be:
<%= form_tag edit_multiple_categories_path, method: :get do %>
I copied that from the railscast #165
Perhaps someone will find same issue and will find this useful.

ROR build inserting mutiple records at a time by form

Form screenshot
the image up explain that there is employ who has been allocated three projects at this time he want to full up the fields as he want example he can fill up 2 fields and press submit btn or 1 or 3 depending how many projects he has been assigned.
what i was unable to do is that i want to insert that multiple fields in a table in single submit i do not understand what code i write it will work
the code in my controller is the following.
def new
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new
#project=Project.where(:employee_id => params[:employee_id]).sorted
end
def create
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new(params.require(:timesheets).permit(:employee_id,:project_id,:IN,:comments))
if #timesheet.save
flash[:notice] = "data entered."
redirect_to(:action => 'show',:employee_id =>#pro.id)
else
flash[:notice]="logged out"
end
end
the code in my new.html.erb is the following
<%= form_for(:timesheets, :url => {:action => 'create',:employee_id => #pro.id}) do |d| %>
<% if !#project.nil? %>
<% #project.each do |page| %>
<tr>
<%= d.hidden_field("employee_id" ,:value => #pro.id) %>
<%= d.hidden_field("project_id" ,:value => page.id) %>
<% if !page.employee_id.blank? %>
<td><%= page.prog_name %></td>
<td><%= d.text_field("IN",:class => "qty1") %></td>
<td><%= d.text_field("comments") %></td>
<% end %>
<% end %>
</tr>
<% end %>
<tr>
<td>Total hours</td>
<td colspan="2"><%= text_field_tag("total")%></td>
</tr>
<tr border="0">
<td ><%= submit_tag("Submit") %></td>
<td colspan="2" border="0"></td>
</tr>

Image tag shows all columns of model

Yesterday i made some experience with carrierwav, all works fine but at the image tag where normally only the image should displayed rails also shows the hole model, with created_at etc. Here you cann see it!
So now my view:
<% #patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>
<td colspan="5">
<%= link_to 'Löschen', [treatment.patient, treatment],
:confirm => 'Sind sie sicher?',
:method => :delete %>
<%= treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
</td>
</tr>
<% end %>
The problem has to be in <%= image_tag paint.name %>
Remove the = from <%= treatment.paintings.each do |paint| %>, that is making you also print the treatment.paintings array.
<% treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>

Ruby on Rails - How to recall a method from a closed do block

I've got this code in my matches index:
<table>
<% #matches.each do |match| %>
<tr>
<td><%= match.team_a %></td>
<td><%= match.team_b %></td>
<td><%= match.score_a %> - <%= match.score_b %></td>
<td><%= match.field %></td>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>
</table>
I want to move the rendered asterisk_message outside the table, but this would mean to put it after the <% end %>, which obviously gives me this error:
undefined local variable or method `match' for #<#:0xb6da40d8>
How can I work it out?
Try:
<table>
<% #matches.each do |match| %>
<tr>
<td><%= match.team_a %></td>
<td><%= match.team_b %></td>
<td><%= match.score_a %> - <%= match.score_b %></td>
<td><%= match.field %></td>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>
</table>
<% #matches.each do |match| %>
<%= render "shared/asterisk_message", :target => [match.team_b, match.team_a] %>
<% end %>

Resources