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.
Related
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 %>
I have the following code:
#items = QuestionGroup.search(params[:search]).limit(50)
This returns an ActiveRecord relation. In the view I want to iterate through it so I use:
<% if #items.present? %>
<%= #items.each do |r| %>
<%= div_for r do %>
<div><%= r.subject %></div>
<% end %>
<% end %>
<% end %>
This does print r.subject to the view but it then follows it with the entire relation. e.g.
the pipe
[#<QuestionGroup id: **, subject: "the pipe", created_at: "*******", updated_at: "******"]
Why is this and how can I fix it?
Problem is here:
<%= #items.each do |r| %>
This line of code iterates over each of the relations and due to the '=' you output its content. Change it to:
<% #items.each do |r| %>
and you are good to go!
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.
<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.
Im new to RoR/Ruby and i cant seem to get the simplest thing to work. (trust me, ive search google and reread docs, i dont know what wrong)
So in my main view, I added the following:
<%= if 1>2 %>
<%= print "helllloooo" %>
<%= else %>
<%= print "nada" %>
<%= end %>
And nothing is outputted..
**UPDATE**
Ok heres my new CORRECTED code and its STILL NOT WORKING
<th>
<% if 1 > 2 %>
<%= print "helllloooo" %>
<% else %>
<%= print "nada" %>
<% end %>
</th>
Your statements are not intended to be displayed so instead of
<%= if 1>2 %>
write
<% if 1 > 2 %>
Same thing for else and end
EDIT
<% if 1 > 2 %>
<%= "helllloooo" %> #option 1 to display dynamic data
<% else %>
nada #option 2 to display static data
<% end %>
You don't need to use print, or even ERB for the text. Also, your if, else, and end statements should be <%, not <%=:
<% if 1 > 2 %>
helllloooo
<% else %>
nada
<% end %>
<%= already means "print to the HTML response" in ERB (Ruby's own templating language).
So <%= print '...' means "print the return type of print '...'" which is nothing.
The right code would look like:
<% if 1>2 %>
<%= "helllloooo" %>
<% else %>
<%= "nada" %>
<% end %>
In fact you can even omit the <%= because you're just printing strings (not arbitrary objects):
<% if 1>2 %>
helllloooo
<% else %>
nada
<% end %>
The = is the problem. Use <% instead. <%= is for printing something, while <% is for instructions.
for dynamic content use: <%= %>