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: <%= %>
Related
Here, I have 10 columns i.e., answer1, answer2, answer3, ..., answer10 in the table MgAnswer.
I have to check whether each column value is present or not. Only if it present,then I have to display it in the page.
Im giving column names dynamically within for loop
<% (1..10).each do |i| %>
<% if MgAnswer."answer#{i}".present? %>
<%= MgAnswer."answer#{i}" %>
<% end %>
<% end %>
Im ending up with Syntax error.
You can indeed dynamically invoke methods in ruby, but this is not the syntax. Instead do
<% (1..10).each do |i| %>
<% if MgAnswer.public_send("answer#{i}").present? %>
<%= MgAnswer.public_send("answer#{i}") %>
<% end %>
<% end %>
It should seem like the following:
<% (1..10).each do |i| %>
<%= MgAnswer.send("answer#{i}") %>
<% end %>
Since ruby can't evaluate line as MgAnswer."method". Also you can just skip if condition, because it will be evaluated to empty string "".
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.
I want to print out a list of links separated by commas in Rails.
Heres what I've got:
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
,
<% end %>
Heres what I want:
Thing A,
Thing B,
Thing C
But right now I get an extra comma on the last iteration of the loop! What should I do?
One way of doing this is with map then Array#join:
<%= topics.map { |topic| link_to(topic.name, topic.link) }.join(',').html_safe %>
if you want to do minimum possible change to your code, you can use the following
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
<% if(topic != topics.last) %>
,
<% end %>
<% end %>
How about using each_with_index, and only put comma before the content unless it's not the first item.
<% topics.each_with_index do |topic, i| %>
<% if i > 0 %>
,
<% end %>
<%= topic.name %>
<% end %>
I made it in one line call (for active records collections) using the concat helper:
<% concat (',') if e.bills.last != b %>
concat is an ERB helper (TextHelper) to add some HTML without the <%= %> syntax, helpful to add few characters.
Here is the full code to make it clear:
<% event.bills.each do |b| %>
<%= link_to(b.number.to_s, bill_display_path(b)) %>
<% concat (',') if e.bills.last != b %>
<% end %>
Simply try this. It works for me
<%= topics.map{|p| p.topic.name}.join(",") %>
You can do the following to print out the comma for all items except for the last:
<% topics.each do |topic| %>
<%= topic %>
<%= "," if topic != topics.last %>
<% end %>
This will check if the current item in the loop is the last item, and will use the <%= %> syntax to output the comma.
This is the code for an address partial I just wrote. People might put single line addresses in either street line, company name is optional, etc... It works exactly how I want it to, but I know that checking each variable twice is ugly and terrible.
<%= "#{a.name}" unless a.name.blank? %>
<% unless a.name.blank? %> <br> <% end %>
<%= "#{a.company_name}" unless a.company_name.blank? %>
<% unless a.company_name.blank? %> <br> <% end %>
<%= "#{a.street_1}" unless a.street_1.blank? %>
<% unless a.street_1.blank? %> <br> <% end %>
<%= "#{a.street_2}" unless a.street_2.blank? %>
<% unless a.street_2.blank? %> <br> <% end %>
<%= "#{a.city}, #{a.state} #{a.zip}" %>
So, my gratuitous use of unless aside, how should I be putting in a conditional line break?
Update:
As discussed below, it is dangerous to use .html_safe on user input. If you do use a helper method as suggested below, you must also sanitize all user input on the way into the database. I've rewritten the code above as:
<% unless a.name.blank? %>
<%= a.name %>
<br>
<% end %>
<% unless a.company_name.blank? %>
<%= a.company_name %>
<br>
<% end %>
<% unless a.street_1.blank? %>
<%= a.street_1 %>
<br>
<% end %>
<% unless a.street_2.blank? %>
<%= a.street_2 %>
<br>
<% end %>
<%= "#{a.city}, #{a.state}" %> <%= a.zip %>
The redundant checking was just me overcomplicating things. I'd strongly recommend against using .html_safe in a situation like this, since you create new problems for yourself: sanitizing the input, and remembering which fields are safe. Better to not override the sensible protection Rails provides.
There are many, many ways to go about cleaning it up, but a helper would be appropriate here:
module ApplicationHelper
def format_address(a)
top = [a.name, a.company_name, a.street_1, a.street_2]
top.reject! {|s| s.blank?} # remove null and empty values
"#{top.join('<br/>')}#{a.city}, #{a.state} #{a.zip}".html_safe
end
end
Then in your view:
<%= format_address(a) %>
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.