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}
Related
I have a small html fragment and want to write a conditional such that the first two loading results are wrapped in a div called "loading-top-two" but not sure how to do it. Was thinking something like this but (obviously) doesn't work. How would I do this? On RoR, if there's any magic to be had there.
.loader-results
- (0..2).each do |i|
- if i==0
.loading-top-two
= render "lawyers/search/loading_result", idx: i
- else
= render "lawyers/search/loading_result", idx: i
If I understand correctly you can just do this, first render the first two and then the rest of the result. [2...] means everything from the item with index = 2 until the end (infinity)
.loading-top-two
- #result[0..2].each_with_index do |result, index|
= render "lawyers/search/loading_result", idx: index
- #result[2...].each_with_index do |result, index|
= render "lawyers/search/loading_result", idx: index
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 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.
I have a collection/array in rails, transformed to json it looks like this:
#collection = [{"order_number":"123","item":"Paper"},{"order_number":"567","item":"Ruler"},{"order_number":"344","item":"Pen"},{"order_number":"342","item":"Pencil"},{"order_number":"877","item":"Keyboard"}]
I would like to pick the item with the order_number "342" and put it at the last position of the collection, so the new collection looks like this:
#collection = [{"order_number":"123","item":"Paper"},{"order_number":"567","item":"Ruler"},{"order_number":"344","item":"Pen"},{"order_number":"877","item":"Keyboard"},{"order_number":"342","item":"Pencil"}]
In theory, it would look like this:
#collection.last = #collection[3]
but that is obviously not fancy ruby style nor would it re-sort the array as in my example.
Also I don't know the index of the item as it can change depending on what the user shops.
how about:
#collection << #collection.delete_at[#collection.index{|x| x[:order_number] == "342"}]
This basically searches the index of element with :order_number 342 first, uses that index to delete it, and then store the deleted element at the end again.
You can also use the partition method:
#collection = #collection.partition { |h| h['order_number'] != '342' }.flatten
Just split your collection on two (without 342 order and with 342 order), then just join them. It should looks like:
#collection = #collection.select {|e| e[:order_number] != '342' } + #collection.select {|e| e[:order_number] == '342' }
If you have an index of an item it boils down to
#collection << #collection.delete_at(3)
If you don't, you could try finding it using
#collection.find_index{ |el| el["order_number"] == "123" }
Alternative you can try this too:
> #collection.each_with_index{ |key,value| #collection.push(#collection.delete_at(value)) if key[:order_number] == "344" }
#=>[{:order_number=>"123", :item=>"Paper"}, {:order_number=>"567", :item=>"Ruler"}, {:order_number=>"342", :item=>"Pencil"}, {:order_number=>"877", :item=>"Keyboard"}, {:order_number=>"344", :item=>"Pen"}]
I have created a loop, to calculate a total rating of a record. To do this I am first looping through all the child records (ratings), extracting the rating from each row, adding it to the total and then outputting the total.
<% total = 0 %>
<% for ratings in #post.ratings %>
<% total = (total + ratings.rating) %>
<% end %>
<%= total %>
My question is, simply, Is this the rails way?
It achieves the desired result, although needs 5 lines to do so. I am worried I am bring old habits from other languages into my rails project, and I am hoping someone could clarify if there is an easier way.
The following, preferably in the controller, will do it succinctly:
#rating = #post.ratings.sum { &:rating }
If that seems cryptic, you might prefer
#rating = #post.ratings.inject(0) { |sum, p| sum + p.rating }
Note, however, that this will fail if any of the ratings are null, so you might want:
#rating = #post.ratings.inject(0) { |sum, p| sum + (p.rating || 0) }
You should generally keep logic out of your views. I would put that code in a helper or a controller, and the call a method to calculate the total
Put the following in your controller, then you just need to use #rating in your view:
total = 0
#rating = #post.ratings.each { |r| total += r.rating }
Or you could move it into the Post model and do something like:
def self.total_rating
total = 0
ratings.each { |r| total += r.rating }
total
end
and then simply call #post.total_rating