Rails syntax error : unexpected keyword_ensure, expecting end-of-input - ruby-on-rails

I am a newbie in rails and i tried creating a forum application based on a tutorial. This is my forum page but i keep getting the error:
syntax error, unexpected keyword_ensure, expecting end-of-input
Extracted source (around line #33):
30
31 <p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>
here is the forum index page which is throwing the error:
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td><h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small><br />
<%=h forum.description %></td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>no posts<% end %>
</td>
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
<% end %></td>
<!-- <% end %> -->
<% if admin? %>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
<p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>

<!-- <% end %> --> what is this doing? a html commented ERB tag will still evaluate. Remove it. if you want to comment ruby code use # instead, like <% #end %>

Properly formatted code goes a long way towards diagnosing problems like this (mismatch and the like). Try out the following:
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td>
<h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small>
<br />
<%=h forum.description %>
</td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>
no posts
<% end %>
</td>
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %></td>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<% if admin? %>
<p><%= link_to "New Forum", new_forum_path %></p>
<% end %>

all I could see wrong is that you set end before it should here
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
<% end %></td>
so try to move it like this
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
</td><% end %>

I think you have the order of opening and closing blocks jumbled up.
if, for are all opening blocks that have to be closed at the appropriate times.
The commented-out end tag that Benjamin mentioned is actually important but misplaced and has to go between your </tr> and </table> tags to close the for forum in #forums.
I prepared a modified version with some realignments, so I could make sense of it more easily. Haven't actually tested it, though.
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td>
<h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small><br />
<%=h forum.description %></td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>
no posts
<% end %>
</td>
<% if admin? %>
<td>
<%= link_to "Edit", edit_forum_path(forum) %>
</td>
<% end %>
<% if admin? %>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<p>
<% if admin? %>
<%= link_to "New Forum", new_forum_path %>
<% end %>
</p>

Related

Dynamic checkboxes to send array to controller only sending first choice

I have a table of data created like so:
<% #products.each do |product| %>
<tr>
<td><%= form_tag push_to_products_path(product), :id =>
'submit_products' do %>
<%= check_box_tag('send[]', product.sellersku) %>
<% end %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
<% end %>
</tr>
I created the nested form_tag because I have a button outside of the form that uses the checkbox data to send to my Product controller. This is the button code:
<div class="row pad_bottom col-md-4 col-md-offset-1 text-center">
<button type="submit" form="submit_products" class="btn btn-primary">
<span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Push
Selected Products
</button>
</div>
The checkboxes are correctly created in the view, but when I test only the first box has the sellersku value.. all the other checkboxes don't pass anything. They should also be passing the sellersku.
Pretty sure its my loop, just not sure what I did wrong??
Declare your form outside of the loop. Right now, you're looping through your #products and creating a form for each one.
<%= form_tag push_to_products_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %></td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag("Push Selected Products") %>
<% end %>
<%= form_tag products_push_to_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag "Push Selected Products" %> #replace this with glyphicon part
<% end %>
routes.rb #assuming your action name is push_to with resources products
resources :products do
collection do
post 'push_to'
end
end

Link in entire table row seems not to use turbolinks

Im referencing to Link entire table row?
I followed the instructions and now have a link in each table row:
<% #patients.each do |patient| %>
<tr onclick="location.href='<%= patient_path(patient) %>'">
<td><%= patient.name %></td>
This generates for example such a link:
<tr onclick="location.href='/patients/18'">
My problem is now that when i click on a link turbolink isnt used and it takes very long to reload the whole page! How do i have to change my code so that turbolinks is used? Thanks
Try any of these versions, turbolinks should pick up any regular link.
<% #patients.each do |patient| %>
<tr>
<td><%= link_to patient.name, patient_path(patient) %></td>
</tr>
<% end %>
<% #patients.each do |patient| %>
<%= link_to patient_path(patient) do %>
<tr>
<td><%= patient.name %></td>
</tr>
<% end %>
<% end %>
<% #patients.each do |patient| %>
<tr>
<%= link_to patient_path(patient) do %>
<td><%= patient.name %></td>
<% end %>
</tr>
<% end %>

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

How to Correctly render partial template in 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>

Render partial scaffold in the index view - Rails

I want exibe the action 'Show' of scaffold in my index using ajax but when i click in show dont appear nothing my files is:
finances_controller.rb
def show
#finance = Finance.find(params[:id])
end
_show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Date update:</b>
<%= #finance.updated_at %>
</p>
<p>
<b>Money:</b>
<%= #finance.money %>
</p>
<p>
<b>Entrance:</b>
<%= #finance.entrance %>
</p>
<p>
<b>Description:</b>
<%= #finance.description %>
</p>
<p>
<b>Situation:</b>
<%= #finance.situation %>
</p>
<p>
<b>Local:</b>
<%= #finance.local %>
</p>
<%= link_to 'Edit', edit_finance_path(#finance) %> |
<%= link_to 'Back', finances_path %>
my
index.html.erb modified the button show:
Bem vindo <%= session[:user_name]%>
<div id='list' align='left'>
<h1>Finanças - Hoje é dia <%= Date.today %></h1>
<%= link_to 'New Finance', new_finance_path %>
<table cellpadding="5" >
<tr>
<th>Data de Inserção</th>
<th>Caixa</th>
<th>Entrada</th>
<th>Descrição</th>
<th>Situação</th>
<th>Local</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #finances.each do |finance| %>
<tr>
<td><%= finance.created_at %></td>
<td><%= finance.money %></td>
<td><%= finance.entrance %></td>
<td><%= finance.description %></td>
<td><%= finance.situation %></td>
<td><%= finance.local %></td>
<td><%= link_to 'Show', finance,:action => 'show',:id => #finance , :remote =>:true %></td>
<td><%= link_to 'Edit', edit_finance_path(finance) %></td>
<td><%= link_to 'Destroy', finance, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
</div>
<div id='tools' align='right'>
<%if #finance %>
<%= render('show') %>
<% end %>
</div>
<%= link_to 'Logout', new_session_url,method: :delete %>
when i click in the show appear the url 'localhost:3000/finances/1' but dont happening nothing when i click this i want click and show my partial 'show' in the same page, i render the partial 'show' if especified the #finance ,
<%if #finance %>
<%= render('show') %>
<% end %>
try to use = render 'finances/show'

Resources