Rails syntax error: unexpected keyword_ensure, expecting keyword_end - ruby-on-rails

<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
Above code seems to be resulting in:
ready.html.erb:13: syntax error, unexpected keyword_ensure, expecting keyword_end
ready.html.erb:15: syntax error, unexpected $end, expecting keyword_end
What's going on? What's wrong with this syntax?

The errors you're receiving more than likely stem from trying to execute a if-else conditional wherein you have an extra <% end %> before <% else %>. Ensure that your conditional follows canonical if-else-end logic like the following:
<% if ... %>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
<% else %>
...
<% end %>

You are using if condition. So you should end it. The basic if conditions syntax for erb is
<% if ...condition.. %>
Statement
<% end %>
You have to decide what are you using ? It is if condition or if-else condition
In you case, there is not <% end %> clause at end so you have to add it.
<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
<% end %> # this is what you have to add

My issue was that I forgot to end a do block when creating a link using link_to. My incorrect code looked like:
<%= link_to("#", :class => "example-class") do %>
Nested HTML goes here
I had forgotten to end the do statement. The correct code looks like:
<%= link_to("#", :class => "example-class") do %>
Nested HTML goes here
<% end %>
Hope this helps someone.

Related

How to specify syntax error in Ruby on Rails

My controller is like this
def show
#receipt = Receipt.find(params[:id])
#hospitalizations=#receipt.hospitalizations
#outpatients=#receipt.outpatients
#surgeries=#receipt.surgeries
end
my show.html.erb is like this.
<h1>details of receiptid: <%= #receipt.id %></h1>
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% #surgeries.each do |surgery| %>
<p>surgeryid:<%= surgery.id %> surgery_day :<%= surgery.surgery_day %> </p>
<% #outpatients.each do |outpatient| %>
<p>outpatientid:<%= outpatient.id %>outpatient_day :<%= outpatient.outpatient_day %> </p>
<% end %>
When I access it's page,some error like below was incurred. I tried to specify error location,but didn't work well.
It seems syntax error,where should end insert?
If someone has experienced same issues,please let me know.
Each of your each loop requires an <% end %> clause, you can't find the specific line since the error says it reached the end when it expected "end". Close all your loops like this:
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% end %>

Hearing in Rails Syntax Error

New to Rails, trying to add a favorites, using hearts on posts in my app and dont know why I'm getting this sytax error. Have followed the tutorial step by step.Is this somethong obvious ?
/Users/leehumphreys/Desktop/with hearts favorites/app/views/rooms/show.html.erb:292: unterminated string meets end of file /Users/leehumphreys/Desktop/with hearts favorites/app/views/rooms/show.html.erb:292: syntax error, unexpected end-of-input, expecting ')'
<% #rooms.each do |room| %>
<%= room.title %>
<%= div_for room do %>
render "hearts/button”, room: room
<% end %>
<% end %
Actually getting the error at <% #rooms.each do |room| %>
First:
<% room.title %>
won't do anything because you need to output the result, thus:
<%= room.title %>
Then, you have one too many end. You only need to close one bock:
<%= room.title %>
<%= div_for room do %>
<%= render partial: "hearts/button”, locals: { room: room } %>
<% end %>
Basically, your end is a completion for do. Every do starts a block, which must end with an end.
Also note your render can be simplified as such:
render "hearts/button”, room: room
Update after comment to answer:
<% #rooms.each do |room| %>
<%= room.title %>
<%= div_for room do %>
<%= render "hearts/button”, room: room %>
<% end %>
<% end %>
I suggest you take a look at some erb tutorial, this one for example.

Ruby on Rails how to get last query?

I'm having trouble trying to figure out when I reached the end of my query. So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. When I try I get a bunch of my custom messages even the query didn't turn out blank. Is there any way to tell if I've reached EOF in ruby on rails?
Sample
<div id = "content-A">
<p>A</p>
<% #animes.each do |anime| %>
<% if anime.aname.starts_with?('A') %>
<%= link_to anime.aname, {:action => 'list'} %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<%end %>
</div>
I believe you want sth like:
<% animes_group = #animes.group_by {|anime| anime.aname.to_s[0].upcase}
('A'..'Z').each do |letter| %>
<div id="content-<%= letter %>">
<p><%= letter %></p>
<% if animes = animes_group[letter] %>
<% animes.each do |anime| %>
<%= link_to anime.aname, {:action => 'list'} %>
<% end %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<% end %>
You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether #animes are being used anywhere else etc.

Rails 4/ERB if/else loop gives unexpected ;, expected : error, if I add html or text

I'm trying to create a mobile app in Rails 4/ERB where people can post and comment on others' posts. I'd like to be able to toggle the visibility of comments using jQuery, but whenever I attempt to wrap the each loop in a div tag, or add comments count, I get an error. ('comments/comment' is a partial containing the template for the actual comment)
This works, but shows all the comments (not what I want):
<% if post.comments.count > 0? %>
<% post.comments.order(:created_at).reverse.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%:%>
No comments yet.
<% end %>
this gives me an unexpected semicolon error:
<% if post.comments.count > 0? %>
<%= post.comments.count %> Comments.
<% post.comments.order(:created_at).reverse.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%:%>
No comments yet.
<% end %>
so does this:
<% if post.comments.count > 0? %>
<div>
<% post.comments.order(:created_at).reverse.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
</div>
<%:%>
No comments yet.
<% end %>
the exact words of the error are syntax error, unexpected ;, expected :
What am I doing wrong, and how can I use HTML within this block without it throwing an error at me? Any help would be much appreciated.
addendum: oddly enough, if I stick the comment count code inside the each loop, it works (but, because it's an each loop, displays it multiple times)
addendum 2: even this gives an error:
<%= render 'comments/form',post: post%></p>
<% if post.comments.count > 0? %>
<!--<%= post.comments.count %> Comments. -->
<% post.comments.order(:created_at).reverse.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%:%>
No comments yet.
<% end %>
Try like this
<%= render 'comments/form',post: post%></p>
<% if post.comments.count > 0 %>
<!--<%= post.comments.count %> Comments. -->
<% post.comments.order(:created_at).reverse.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%else%>
<!-- No comments yet. --->
<% end %>
remove ? in 2nd line because post.comments.count > 0 this will return true or false,so need to check again.
so,now you can use else condition.
As others have alluded to, the snippet if post.comments.count > 0? implies that you want to use the ruby ternary ?: operator. As its name implies, the ternary ?: operator is composed of 3 parts:
the condition
value if the condition is true
value if the condition is false
Here's an example of its use:
is_even = (num % 2) ? true : false
Note that in this example both the if check (?) and else (:) are included, as is required by the ternary operator. So the issue with your use of ? is that you're using the if check, but not the else check.

How to add a line break with conditional operators

OK, stupid newbie question: how do you make a line break only when it's needed?
I'm creating a basic address listing and only want to include a line of an address if it isn't blank. How do I keep the blank line from printing? I've tried including the break and new line tags, and tried using puts and quotation marks of both varieties and escaping the slashes but can't seem to display the address correctly.
Is there a way to have each line of the address to print on its own line or simply omit the line if there is no info to put on it?
Here's the current version of the code:
<p><strong>Main Address</strong></p>
<p><%= if #vendor.address1 || null
#vendor.address1 #need a break here
end %>
<%= if #vendor.address2 || null
#vendor.address2 #need a break here
end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>
This is how I would do it:
<p>
<strong>Main Address</strong>
</p>
<p>
<% unless #vendor.address1.blank? %>
<%= #vendor.address1 %><br>
<% end %>
<% unless #vendor.address2.blank? %>
<%= #vendor.address2 %><br>
<% end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %>
</p>
By the way: the || null in your code is not valid Ruby. null does not exist, it should be nil. But even if you had used nil, your code does not do what you expect it to do. For these kind of things, you'd better use blank?.
<p><strong>Main Address</strong></p>
<p><%= #vendor.address1%><%= <br/> if #vendor.address1.blank? %>
<%= #vendor.address2%><%= <br/> if #vendor.address2.blank? %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>

Resources