I'm having a problem getting the syntax correct. The html helper returns true or false.
<% if (<%= Html.SecurityTrim("Admin")%>) { %>
<span>Only for accounting</span>
<% } %>
What do I need to change to get this to compile correctly?
Supposing that Html.SecurityTrim() returns bool,
<% if ( Html.SecurityTrim("Admin") ) { %>
<span>Only for accounting</span>
<% } %>
Sounds like your Html.SecurityTrim() returns a bool.
In this case, then no need to escape out of code, just use it as your condition to test.
<% if (Html.SecurityTrim("Admin")) { %>
<span>Only for accounting</span>
<% } %>
Related
I am trying to output just the date on the generated line, but when I execute this code, which I am sure is not elegant at all, it gives me undefined method 'strftime' for nil:NilClass
<% #person.subordinates.each do |sub| %>
<tr>
<td>
<% if sub.position == 'alumni' %>
<p>
<%= link_to(sekret_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %>,</strong>
<% end %> <%= sub.startdate.strftime("%Y") %> - <%= sub.graddate.strftime("%Y") %>
</p>
<% end %>
</td>
</tr>
<% end %>
Further up in my code, I am using #person.startdate.strftime("%m/%d/%Y") without any issue, but I think my issue is that I am using the sub call, which is grabbing any people that are flagged as sub to this person.
Your error says that one of your attributes is nil. The sytax is correct. Try debugging with:
#person.subordinates.select { |sub| sub.startdate.nil? || sub.graddate.nil? }
The elements that are retrieved on this line will be the ones that are giving you errors, because you are trying to run nil.strftime('%Y'), which makes sense.
You should use a condition on your .erb to stop it, like:
<%= sub.startdate && sub.startdate.strftime("%Y") %> - <%= sub.graddate && sub.graddate.strftime("%Y") %>
It is not an error with Rails.
strftime is being called on nil object. Make sure you are calling strftime on a date, time, or datetime object.
Refer to strftime on rails apidock - https://apidock.com/ruby/DateTime/strftime
Another way to solve this that will guard against nils would be:
<%= sub.startdate&.strftime("%Y") %> - <%= sub.graddate&.strftime("%Y") %>
That way you don't have to add a conditional to make sure that any method/attribute in the chain exists prior to calling them. This works because of the safe navigation operator, which is explained in depth here: https://rubyinrails.com/2017/11/17/safe-navigation-operator-ampersand-dot-in-ruby/
I was trying to refactor my erb code for rating stars into one line and came across this, is it possible to achieve the results another way?
So currently it's like this
<% rating.times do %>
<%= image_tag 'rating_star.png' %>
<% end %>
Ideally I'd like to reduce this to
<%= rating.times { image_tag 'rating_star.png' } %>
This returns the value of rating (0, 1, 2, 3, 4 or 5), if I change <%= to <% nothing is rendered. Is there any way to do this?
You can use concat:
<% rating.times { concat image_tag('rating_star.png') } %>
But <%= ... %> within a loop is cleaner IMO.
You need #map to return an array of all image_tags you wish.
Then #join the result to get a string.
Lastly, you need to tell this string is valid html with #html_safe.
<%= rating.times.map{ image_tag 'rating_star.png' }.join.html_safe %>
The difference between <% %> and <%= %> is that the former is simply evaluated, but with the latter the result is rendered. You have two things going on in your code snippet: the control part (the loop), and the rendering part (the image tag). Since you want only the image tag part rendered, you want to surround only that code with <=% %>, and the rest with <% %>.
So while this would work:
<%= rating.times { %> <%= image_tag 'rating_star.png' %> <% } %>
I find it much less readable than the 3-line version.
In my rails app, I'm trying to check if one string (current_user.email) is equal to another string, (#user.email).
How would I do this with an if/else statement?
I tried
<% if #current_user.email(=#user.bio) %>
Link
<% else %>
<% end %>
but I got a syntax error. help?
The invalid syntax is (=#. The = is for assignment and has no use in method invocation.
Your if line should look like
<% if #current_user.email == #user.email %>
<% if #current_user.email == #user.bio %> try this & see.
In reference to this
I've created a question in a webform like this:
<div class="form_row">
<label for="features[]">Features:</label>
<% [ 'scenarios', 'role_profiles', 'private_messages', 'polls' ].each do |feature| %>
<br><%= check_box_tag 'features[]', feature,
(params[:features] || {}).include?(feature) %>
<% end %>
</div>
So if scenarios and private_messages gets checked and I print out params[:features] I will get:
scenariosprivate_messages
I was wondering how would I be able to obtain scenarios and private_messages separately from params. Is the mapping params[:features] = "scenariosprivate_messages" or is it really params[features] = ["scenarios", "private_messages"] ? If it's the latter how can I loop through them?
I write in my view:
<%= params[:features].each {|param|
param.capitalize
} %>
and I still just get scenariosprivate_messages printed.
Try this instead:
<% params[:features].each do |param| %>
<%= param.capitalize %>
<% end %>
The problem with your original solution is that you're printing out the result of the block, which is the array itself, rather than printing out each element of the array.
You shouldn't be using params in your views. You're best off assigning params[:features] to an instance variable in your controller and then iterating over that in your view.
But to answer your question, you're putting the equals sign for output in the wrong place. You want to output each element of the array individually instead of outputting the result of the loop.
You must use humanize:
<% params[:features].each do |param| %>
<%= param.humanize %>
<% end %>
According to this blog post you should be able to access them individually as params[:features]['scenarios'] etc. Looping should just work like with all other arrays -- eg
params[:features].each { |param|
# do something with param
}
In asp.net mvc, when do we use:
and
Do we ever need to put a ; (colon) ?
<%= %> renders the output (string) of the contained command to the response. <% %> wraps executable statements (logic) in the view to control what gets executed. You don't use semicolons in the <%= %> blocks, but may in the <% %> depending on what statements are included.
String rendering:
<%= Html.Encode( Model. Property ) %>
Code block:
<% Html.RenderPartial( "ViewName" ); %>
EDIT: Here's a link to the reference.
<%="something" %> is just a shortcut for Response.Write("something")
is used when you are calling some HtmlHelper method which returns a string e.g.:
is used when you are calling some HtmlHelper method which is void: