I m working on app and i have a question for you. I m trying to display field for a specific value. I have 2 value "oui" and "non". I m done an if condition but doesn't work, value "non" is always display. I don't want to display this.
My value in db
Plage = oui
Etang = oui
Montagne = oui
Riviere = non
<% #camping.situations.each do |situation| %>
<% if situation.plage == "oui" || situation.etang == "oui" || situation.montagne == "oui" || situation.riviere == "oui" %>
<p> Plage: <%=situation.plage %> à <%=situation.distanceplage%> km</p>
<p> Etang: <%=situation.etang%></p>
<p> Montagne: <%=situation.montagne %></p>
<p> Rivière: <%=situation.riviere%></p>
<% else %>
<%end%>
<%end%>
Can you help me ?
Your 'if' check will evaluate to TRUE when any of you variables are equivalent to "oui".
In your exmple, Plage is == "oui". This means that your IF check will ignore the next checks for the other variables because you are using OR checks. This is called Short Circuiting (https://en.m.wikipedia.org/wiki/Short-circuit_evaluation) in programming.
Consider using an "&&" statement instead of "||". That way when any one of us your variables is "non", it won't display that view.
Related
I need to iterate through a database table where it's conditions will change:
<%# if #story.category == 5 %>
<% #users.where(generalLabour: 1).find_each do |user| %>
<% else %>
<% #users.each do |user| %>
.... Iterate and display data.....
So I need to do something like this, unfortunately this does not seem to work as I cannot run statements like this alongside else statements
I also need to do integer comparisons:
<% #users.where(starttime: > 12 ).find_each do |user| %>
How I do something like this?
Maybe I should do all this in Javascript instead of rails?
Try this
query= ""
if #story.category == 5
query = "generalLabour = 1"
elsif #story.category == 4
query = "generalLabour = 2"
elsif #story.category == 6
query = "generalLabour = 3"
end
#users.where(query).each do |user|
.........
end
query= ""
if #story.category == 5
query = "SaturdayS <= #{story.startTime}"
elsif #story.category == 4
query = "SaturdayE >= #{story.endTime}"
end
#users.where(query).each do |user|
.........
end
You can use a hash with mappings:
x = { 5 => 1, 4 => 2, 6 => 3 }[#story.category]
Or use case statement (which is the ruby version of switch):
x = case(#story.category)
when 5
3
when 4
2
when 6
3
else
raise "Doh! I did not think of this."
end
But it is very likely that you are just doing it wrong. Hardcoding IDs in your application is not a good solution and you should instead solve this by setting up a proper association and getting the related items through it.
I have implemented query on distant lat long and create virtual field in query like this
user = User.all
if longitude.present? && latitude.present? && fulladdress.present?
distantQuery = "(3959 * acos( cos( radians("+latitude+") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians("+longitude+")) + sin( radians("+latitude +")) * sin(radians(latitude)))) < " + miles
user = User.select("*, (3959 * acos(cos(radians("+latitude+")) * cos( radians(latitude)) * cos(radians(longitude) - radians("+longitude+")) + sin(radians(" +latitude +")) * sin(radians(latitude)))) as distant").where(distantQuery)
end
when my if condition run then distant is coming other wise it will not coming
<% #result.each do |r| %>
<tr>
<td>
<%= r.distant.present %>
</td>
</tr>
now in view my distant is come form query then it will show result and when it will not come then it show me error
undefined method `distant' for #<User:0x000000092a6558>
I have implement "present" in view
<% if r.distant.present? %>
<%= r.distant.to_i %>
<% end %>
but still it show me error, What will i use to check if distant is coming then it will show and other wise not ?
this should work:
<% if r.respond_to?(:distant) %>
however this is too much logic in the view, i suggest you refactor some of your code to have very simple and safe methods in your view
Try this
<%= r.try(:distant) %>
I would use another approach:
user = if longitude.present? && latitude.present? && fulladdress.present?
distantQuery = QUERY_BODY_HERE
User.select(SELECT_QUERY_HERE).where(distantQuery)
else
User.all
end
unless user.respond_to(:distant)
class << user
def distant ; nil ; end
end
end
The advantage of this approach is that you have consistent User class, responding to #distant correctly regardless of whether it was calculated.
I need help understanding the usage of elseif statements in the view for Rails. I have done it before but now I am doing it with SLIM and I can't seem to get it to work. I want it to print out the word "him" or "her" depending on the current user gender. The code I am using is printing out the elsif statement in text.
a.profile_btn href="#" data-mfp-src='#ask_me'
img alt="" src="/assets/login_icon2.png" /
if current_user.gender == "male"
span Ask Him Out
elsif current_user.gender == "female"
span Ask Her Out
The original code:
a.profile_btn href="#" data-mfp-src='#ask_me'
img alt="" src="/assets/login_icon2.png" /
span Ask This User Out
Try
- if current_user.gender == "male"
span Ask Him Out
- elsif current_user.gender == "female"
span Ask Her Out
I currently have this:
<% if h.ab_template.AB_filterParent == 0 && h.status != 'geo_enabled' %>
What I'd like to do is say whether if h.ab_template.AB_filterParent == 0 || nil. How would I do that?
I tried this but it wasn't working:
<% if (h.ab_template.AB_filterParent == 0 || nil) && h.status != 'geo_enabled' %>
Would love to know what I've mistyped or implemented incorrectly!
Cheers
If you are sure that h.ab_template.AB_filterParent will always be a number (can be wrapped by quotes), then you can try following
<% if h.ab_template.AB_filterParent.to_i == 0 && h.status != 'geo_enabled' %>
else, if there is a possibility that h.ab_template.AB_filterParent can be something like "abc", "0asd" etc then try:
<% if (h.ab_template.AB_filterParent.nil? || h.ab_template.AB_filterParent == 0) && h.status != 'geo_enabled' %>
Generally nil and zero shouldn't mean the same thing. Try to eliminate the possibility of AB_filterParent being nil before you hit this code, by assigning a default value of zero in your migration table. I don't know how your model is so I can't even show an example of how to do it.
The main problem of using to_i == 0 is that it only works if AB_filterParent is either an integer or nil.
0.5.to_i == 0
"asdasd".to_i == 0
So it's odd.
Another way is to have it initialized in an action or other model method or even after_create callback.
I have an array 'sub_status_arr' which contains a set of values.
If all elements in that array are 52, a button is displayed, else something else is displayed.
I tried the following but it does not seem to work properly. It is only checking if the array contains 51 and ignoring the rest
<%if sub_status_arr.include? 51 || 53 || 54 %>
display button
<% else %>
do something else
<% end %>
How do I check if all elements in the array are 52?
Thanks for your suggestions
I would try the all? function:
if sub_status_arr.all? {|ss| ss == 52}