show a text instead of NULL value - ruby-on-rails

3.5 and want to show a text instead of NULL value in my category_id field
<% if e.categories_id == "NULL" %>
<%= e.categories_id ="Don't have value" %>
<% else %>
<%= e.categories_id ="Has value" %>
<% end %>
Someone can help me please?
I have tried
<% if e.categories_id = NULL %>
<% if e.categories_id == NULL %>
<% if e.categories_id = "NULL" %>
<% if e.categories_id == "NULL" %>

<%= e.categories_id.nil? ? "Don't have value" : "Has value" %>
or even better:
<%= e.categories_id ? "Has value" : "Don't have value" %>

<%= e.categories_id || "There's no categories_id" %>
But seriously, there's so much that's bad about this question. Firstly, you should know enough Ruby to know that it uses nil not NULL. A foreign key called categories_id suggest you have a model called Categories. Model names should be singular.

<% if e.categories_id.blank? %>
<p>"Don't have value" </p> (or whatever you want displayed)
<% else %>
<%= e.categories_id %> (should display the value of e.categories_id)
<% end %>
I prefer the .blank? method that Rails uses to check for both .empty? and .nil? Plus using an if else is more readable than the Terinary operator ? :

You can do this if you only want to display "Don't have value" if that is the case:
<%= e.categories_id || "Don't have value" %>
What you could do was to set the value:
<% e.categories_id ||= "Don't have value" %>
<%= e.categories_id %>
If you want to change the value of the data, you should not do that in the view. You should do all the data-manipulation in the controllers or even better, the model (fat model, skinny controller).
There is one other issue you are having: In Ruby null is called nil. So a test for nil would look like this:
if whatever == nil
#Do anything
end
What is nice about Ruby is that nil is considered as false in a statement, so you can omit the == nil so you get:
if whatever
#Do anything
end

Related

If the value of the nested input

I'm trying to check a value (:cat) form a nested input. If this value is one, display something, else dispay nothing
<%= f.simple_fields_for :elements do |element| %>
<%- if element.cat == 1 %>
<%= render 'elements/element_fields', f: element %>
<% end %>
<% end %>
I tried that, but is not working. I have the error (undefined method `cat'). Do you have an idea to solve that ?
Ask not form object but original one
if element.object.cat == 1

Is there a way to get the console to ignore certain errors?

Say I have an instance variable #n, and I'm calling <%= #n.title %> in my view.
If #n equals a valid record, then this will print normally. But if #n is blank or invalid, then the entire page will show an error message, because of this one little line.
Is there a way to get #n.title to just print nil if #n is nil or invalid?
I'm looking for a way to do this without conditional statements. For example, if I wanted to print
<%= #v1.title %>,<%= #v2.title %>,<%= #v3.title %>,<%= #v4.title %>,
if I wanted to use conditionals to print without errors, it would require 12 lines of code:
<% if #v1 %>
<%= #v1.title %>,
<% end %>
<% if #v2 %>
<%= #v2.title %>,
<% end %>
<% elsif #v3 %>
<%= #v3.title %>,
<% end %>
<% elsif #v4 %>
<%= #v4.title %>,
<% end %>
It seems a shame to use 12 lines on this. It would be nice to be able to accomplish the error-handling right when printing.
You can totally do this easily with the try() method. I use it all the time.
<%= #n.try( :title ) %>
That will return nil if #n is nil or if the title method doesn't exist on #n.
You can also chain them together like this:
#n.try( :title ).try( :to_s )
Or even use it on a hash:
#n.try( :[], 'name' ) # Which is the same as #n['name']
See http://api.rubyonrails.org/classes/Object.html#method-i-try
EDIT (Jan 11, 2016)
You can now use the "safe navigation operator" as of Ruby 2.3.0.
#n&.title&.to_s
As well as the Array#dig and Hash#dig methods introduced in Ruby 2.3.0.
hash = { 'name' => 'bob' }
hash.dig( 'name' ) # Which is the safe way to do hash['name']
You can add some logic to your view that differentiates between development (where some errors can be ignored) and production environments (where errors should cause your app to fail in an obvious and ugly manner). Ruby's nil has a "falsey" nature, so you can use that concept to your benefit as well.
<% if Rails.env.development? %>
<% if #n %>
<%= #n.title %>
<% else %>
<%= nil %>
<% end %>
<% else %>
<%= #n.title %>
<% end %>

Ruby on Rails Params comparing not working in If else

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 do I check if a string is equal to another string?

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.

simple rails code optimisation

<% 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?

Resources