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
Related
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 to render either an or a depending on a condition, however, slim's indentation and "no explicit end allowed" gets in the way.
In this simplified code, I'd like to wrap a product with three DIVs in either an A or a SPAN depending on whether the product is available or not.
- if available?
a href=...
- else
span class="unavailable"
.currency = #product.currency
.price = #product.price
.description = #product.description
The above obviously won't work since the product is not rendered within the A or SPAN but next to it.
- if available?
a href=...
- else
span class="unavailable"
.currency = #product.currency
.price = #product.price
.description = #product.description
This case is even worse, because slim treats the product as part of the "else" case and renders the product next to the SPAN and not at all for the A.
- if available?
a href=...
- else
span class="unavailable"
.currency = #product.currency
.price = #product.price
.description = #product.description
Here everything is fine for the SPAN but the A remains empty because the product is treated as part of the "else" case.
- if available?
a href=...
- else
span class="unavailable"
- end
.currency = #product.currency
.price = #product.price
.description = #product.description
Now, this could be correct, but slim doesn't allow explicit "end" and therefore raises an exception.
Any idea how I can get this to work?
PS: Maybe slim should allow explicit "end" for edge cases like this one.
This is what I could come up with as a workaround:
Using capturing to local variables you save your children in a local variable:
= capture_to_local children=:children
.currency = #product.currency
.price = #product.price
.description = #product.description
And now you can run
- if available?
a href=...
= children
- else
span class="unavailable"
= children
I don't have slim set up, just tried it with haml, but it should work just the same :)
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 want to set some background color to parts of the content in a cell in a Prawn table.
My code looks like this:
#file: /show.pdf.prawn
pdf.table([ ["Type XY", "150", "1245.45"],
["Type ZA", "100", "1243.50"],
["Type BC", "20", "645.00"] ])
Only the XY, ZA, and BC should have a corresponding background color. In HTML I would write: <span style="background: yellow;">XY</span> - but this inline format is not supported yet by Prawn.
The only hint the Prawn manual is giving me is: text -> formatted callbacks. But this doesn't work in a table.
Is there any possibility to do this? Is there any equivalent to the HTML span? Should I try a bounding box or an inner table?
Try
rows = [["Type XY", "150", "1245.45"],
["Type ZA", "100", "1243.50"],
["Type BC", "20", "645.00"]]
pdf.table(rows) do
column(0).background_color = "708DC6" #the color
end
I suggest you to create another stand-alone model for prawn like on this tutorial.
Hope can help.
Just came across code that can help you with this. Format by Cell instead of Column on the Table.
pdf.table(rows) do
cells.style do |c|
if c.column == 0 and c.row == 0
c.background_color = "708DC6"
elsif c.column == 1 and c.row == 0
c.background_color = "2944ce"
elsif c.column == 2 and c.row == 0
c.background_color = "008000"
end
end
end
Now only the header row has colors and you can choose per column.
<span class=#Model.TotalSent<0?"small":"big">show me only if #Model.TotalSent >0 </span>
The above line is a code that I expect class="big" will be rendered if #Model.TotalSent>0
else class="small" will be rendered.
But is not happening
What is the real syntax to do this?
Try this:
<span class='#(Model.TotalSent < 0 ? "small" : "big")'>show me only if #Model.TotalSent >0 </span>
Try surrounding your expression in parentheses, and make sure you put quotes around your class.
<span class='#( Model.TotalSent < 0 ? "small" : "big" )'>...
class="#(Model.TotalSent < 0 ? "small":"big")"