I am using the ternary operator for single-line conditions, with erb printing tag <%= %>
<p>Status:
<%= notification.read? ? "Read" : link_to "Mark as Read", "/" %>
</p>
I need to give a mark as read link in false condition, in the above scenario getting the syntax template error, here notification is an object of the Notification model.
I want output as-
mark as read
mark as read would be link.
thanks!
Don't use a ternary:
<% if notification.read? %>
Read
<% else %>
<%= link_to 'Mark as Read', '/' %>
<% end %>
Or use parentheses in the link_to method call:
<%= notification.read? ? 'Read' : link_to('Mark as Read', '/') %>
Remember that anything inside <%= ... %> is just Ruby and that link_to is just a Ruby method like any other.
Related
I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact
<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.
Using Rails 3. This is a front-end design question.
Goal:
Contact | Email | URL
show.html.erb:
<% if !#shop.contact.blank? %>
<%= #shop.contact %>
<% end %>
<% if !#shop.email.blank? %>
<%= #shop.email %>
<% end %>
<% if !#shop.url.blank? %>
<%= link_to #shop.url, #shop.url, :target => "_blank" %>
<% end %>
How do I put in | only when the previous and after element has values? At current stage, if there is no value, nothing is output.
Many thanks.
<% url = link_to(#shop.url, #shop.url, :target => "_blank") if #shop.url.present? %>
<%= [#shop.contact, #shop.email, url].select(&:present?).join(" | ") %>
This creates an array of all your elements, selects those which have a value (as present? is the opposite of blank?) and then joins each element of the remaining array by putting a pipe between them.
That said, if you have more complex logic, you should probably create a helper method. The ERB templates are not a good place for complex logic. This above is bordering acceptable.
If I write something like:
<% if signed_in?.blank? %> or <%= link_to "Sign Up", sign_up_path %>
What is the difference between the two signs of <% and <%=?
Why make it this way instead of using just one for simplicity?
When do I know I need to use <% over <%=?
<%= puts the return value of the code inside to the page.
<% just execute code.
Here is the good guide about ERB http://api.rubyonrails.org/classes/ActionView/Base.html
<% %> Simply executes the statement(s) inside that block, whereas <%= %> will output the result of the statement.
So for example, with the <% if signed_in?.blank? %>, the ruby interpreter just executes that code and checks if signed_in is blank.
The <%= link_to %> statement will actually generate HTML.
I have something like this:
<p>
<b>Tags:</b>
<%if #post.tags.count > 0%>
<%= #post.tags.collect {|c| (link_to c.name, c)}.join(", ")%>
<%else%>
Does not have any tags.
<%end%>
</p>
Which gives me
Tags: Java, CSS
Instead of Java and CSS links. What am I missing?
It's because strings in Rails 3 are, by default, not considered HTML-safe. See this blog post about it.
You can manually mark something as safe by calling .html_safe on it, which would make your code like so:
<p>
<b>Tags:</b>
<%if #post.tags.count > 0%>
<%= #post.tags.collect {|c| (link_to c.name, c)}.join(", ").html_safe %>
<%else%>
Does not have any tags.
<%end%>
</p>
But I'd recommend doing this instead:
<p>
<b>Tags:</b>
<% if #post.tags.count > 0%>
<% #post.tags.each_with_index do |tag, i| %>
<%= link_to h(tag.name), tag %><%= ', ' if i < #post.tags.size - 1 %>
<% end %>
<% else %>
Does not have any tags.
<%end%>
</p>
I think html_safe is what you are looking for!
So this would solve the problem (#post.tags.collect {|c| (link_to c.name, c)}.join(", ")).html_safe
I think your tag names should be input by the user, right?
In this case, html_safe is not your first choice, as it gave full trust to the user. And your site would encounter XSS attacks.
The better choice should be sanitize. See the reference here: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
As you only want to have links, the following line do what you want:
<%= sanitize #post.tags.collect {|c| (link_to strip_links(c.name), c)}.join(", "), :tags => %w(a) %>
Note the use of strip_links(c.name), this removes all the links that input by the user.
Let's assume the tag names are: ["Product", "hi", "bye"]
By just using .html_safe, the following would be shown:
Product, <strong>hi</strong>, <a href='bad_site.com'>bye</a>
But using the mix of sanitize with strip_links, the following is the result:
Product, <strong>hi</strong>, bye
Or you could mix the use of strip_tags with .html_safe:
<%= #post.tags.collect {|c| (link_to strip_tags(c.name), c)}.join(", ").html_safe %>
This simply removes all tags in c.name before you call the html_safe.
I would suggest (and you probably are doing already :D) removing all unwanted tags before storing into the database.