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.
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 ran into a very strange problem . I am trying to use if and else in rails it works perfect when I am comparing current_user.id with numbers but when I am trying to do it with params it did not work . Here is my code which work :
<%= #u = 2 %>
<% if current_user.id == #u %>
ssssss
<% else %>
aaaaaaaaa
<% end %>
And when I am trying to use this code :
<%= #u = params[:id] %>
<% if current_user.id == #u %>
ssssss
<% else %>
aaaaaaaaa
<% end %>
It is always giving me aaaaaaaaa value it would be helpful if anyone can help me to fix this
Use current_user.id == #u.to_i rather then #u because params gets a string and you compare with an integer.
Or you can compare by using current_user.id.to_s == #u
Here you are trying to compare string with number . Above answer is sufficient but I would like to correct your code simply add this to your code and you are done :
<%= #u = 2 %>
<% if current_user.id == #u.to_i %>
ssssss
<% else %>
aaaaaaaaa
<% end %>
The correct answer has been given. I usually, in cases like this, like to output the variables I'm interested in with ".inspect". This usually gives a clear information about what goes wrong.
In your specific case, this would have output 3 for the id and "3" for the param - easy to see that way.
How can I condense my code into a single statement:
<% #policyholderdetail.errors.each do |attr,msg| %>
<% if attr == :title %>
<li><%=attr %> <%= msg %></li>
<% end %>
<% end %>
I would like to show only the errors for :title next to the field but feel there should be a better statement to do this as opposed to looping through all of the errors until I get to the one I want.
Question - can I condense the first two lines into one better statement?
You can write: #policyholderdetail.errors[:title]. See here.
Use
<% if #policyholderdetail.errors[:title].present? %>
<% if defined?(#club.name) && !(#club.name).nil? %>
<%= #club.name %>
<% else %>
clubs:
<% end %>
first line looks ugly. any help is appreciated.
Perhaps:
<% if #club && #club.name %>
<%= #club.name %>
<% else %>
clubs:
<% end %>
Since #club will presumably either be set to a Club object or be set to nil, we don't really need to explicitly check whether or not the #club has a name method. Instead, we can just test that #club is set at all, and a truthiness test will suffice for that.
Similarly, we don't need an explicit nil check on #club.name. All values other than false and nil are considered "truthy," and, since a club's name presumably can't be set to false, simply testing its truthiness is equivalent to explicitly checking if it is nil.
Violet pointed out in the comments that this is actually a fairly common idiom in the Rails world, so a shortcut actually exists. In a Rails environment, all objects, including nil, have a method named try. try invokes the method name passed to it as an argument, and either returns that method's return value, or nil if that method is not defined.
So, for example:
no_name = Club.new
no_name.try(:name) # => nil
so = Club.new :name => 'StackOverflow'
so.try(:name) # => "StackOverflow"
nil.try(:name) # => nil
As such, the following is the exact equivalent to the first code block:
<% if #club.try(:name) %>
<%= #club.name %>
<% else %>
clubs:
<% end %>
Much prettier :)
<%= #club.name || "clubs:" %>
how about that?
I expect to see a nil returned with the following embedded Ruby:
<%=h [#inventory.origin.code] %>
it returns a "NoMethodError nil object". However when an object is in fact present, it functions just fine (as expected).
Therefore I created this test (following this advice):
<b>origin_id:</b>
<% if (#inventory.origin.code.nil? or #inventory.origin.code == 0) %>
<%=h [#inventory.origin] %>
<% else %>
<%=h #inventory.origin.code %>
<% end %>
unexpectedly rails returns NoMethodError "You have a nil object when you didn't expect it!"
Do you have any suggestions in dealing with this situation? Thanks!
You are adding the .nil? check too late. I am assuming it is origin that is nil, not code or you wouldn't be getting that error. Use this instead:
<%=h [#inventory.origin.code] unless #inventory.origin.nil? %>
Refactor of dcneiner's answer:
<%=h [#inventory.origin.code] if #inventory.origin %>