I have a problem with "if":
<%= print 'Hi American people' if #people.country == "Usa" %>
When I preview my app, I cannot see the message "hi american people": the field is blank.
Using print will print to the console and won't actually display anything in your HTML file.
Below, we can use an if "block". In the "block", you can put any HTML within.
<% if #people.country == "Usa" %>
Hi American people
<% end %>
For example, I can do the following, adding multiple HTML elements within. They will only display if the conditional passes:
<% if #people.country == "Usa" %>
<span>Hi American people</span>
<p>Today is a good day..</p>
<% end %>
Related
I'm relatively new to VB and I'm being forced to use the in-line coding method. I would like to check for a not null value for a variable and then display an image. That's all I want it to do. Here's a couple of instances of what I've tried to do.
<% if ACTL is not nothing then %>
<img id="ACTLLogo" src="<%= ACTL %>" />
<% else end if %>
or this:
<% if ACTL is nothing then %>
<% else %>
<img id="ACTLLogo" src="<%= ACTL %>
<% end if %>
or this:
<% if String.IsNullOrEmpty(ACTL) then %>
<% else %>
<img id="ACTLLogo" src="<%= ACTL %>
<% end if %>
When I do just the part without the if statements, the logo shows up fine, so I'm thinking I just don't know how to do in-line coding IF statements in ASP Classic. Any thoughts?
To check whether a variable is NULL, use the IsNull function:
If Not IsNull(ACTL) Then
Response.Write "<img src='" & ACTL & "' id='ACTLLogo'>"
End If
You can also check whether the variable is EMPTY (which is what you get if, for example, you do a Request.Form("fld") and the form does not have a field called fld) using the, you guessed it, IsEmpty function:
If IsEmpty(Request.Form("fld")) Then
Response.Write "<p class='msg'>No such field!</p>"
End If
If all you care about is whether the variable has a value, any value, then you can do a quick-and-dirty string conversion:
If ACTL & "" <> "" Then
...
End If
This latter trick is very useful for avoiding the type mismatch errors you can get if a variable is unexpectedly NULL, without having to add If Not IsNull(...) all over the place.
In ASP Classic you can use <% if ACTL <>"" %>
You could also write your code like this, which removes the need to close the asp tag.
<%
if ACTL <>"" then
response.write("<img id=""ACTLLogo"" src="""&ACTL&""" />")
end if
%>
Might be a bit easier to read and maintain.
I've created a hash through a loop that looks like this.
<% ship_hash = {} %>
<% #order_preview.fedex_rates.each do |rate| %>
<% if rate.service_name == "FedEx Ground Home Delivery" || rate.service_name == "FedEx 2 Day" || rate.service_name == "FedEx Standard Overnight" %>
<% ship_hash["#{rate.service_name}"] = "#{number_to_currency(rate.price.to_f / 100)}" %>
<% end %>
<% end %>
<% #order_preview.usps_rates.each do |rate| %>
<% if rate.service_name == "USPS Priority Mail 1-Day" %>
<% ship_hash["#{rate.service_name}"] = "#{number_to_currency(rate.price.to_f / 100)}" %>
<% end %>
<% end %>
What I am trying to do is simple. I want to get the output of the keys and values through a block.
>> ship_hash
=> {"FedEx Ground Home Delivery"=>"$9.78", "FedEx 2 Day"=>"$20.59", "FedEx Standard Overnight"=>"$33.78", "USPS Priority Mail 1-Day"=>"$5.60"}
Ok, that's what I expected...
>> ship_hash.each { |key, value| puts "#{key}: #{value}" }
I get
=> {"FedEx Ground Home Delivery"=>"$9.78", "FedEx 2 Day"=>"$20.59", "FedEx Standard Overnight"=>"$33.78", "USPS Priority Mail 1-Day"=>"$5.60"}
When I thought I would get something like this
FedEx Ground Home Delivery: $9.78...
On the RubyMonk primer page here is why I am confused. I don't get the output I expect!!
I am running the commands from better_errors after I <%=raise %> after the code generates hash.
I must be missing something, this is basic and unexpected... Feel free to suggest alternate titles to help future confundos ...
Update
Well it sounds like a live shell isn't the place to run these commands, as it only returns the hash itself. What I am trying to do in practicality is populate a f.select field with both the key and value of the hashes. Not to change the question too much, but How could I populate the option of a select field so it displays the key and values next to eachother?
If you go in irb (or rails console) and type the following:
ship_hash = {"FedEx Ground Home Delivery"=>"$9.78", "FedEx 2 Day"=>"$20.59", "FedEx Standard Overnight"=>"$33.78", "USPS Priority Mail 1-Day"=>"$5.60"}
ship_hash.each { |key, value| puts "#{key}: #{value}" }
You'll see the following output:
FedEx Ground Home Delivery: $9.78
FedEx 2 Day: $20.59
FedEx Standard Overnight: $33.78
USPS Priority Mail 1-Day: $5.60
=> {"FedEx Ground Home Delivery"=>"$9.78", "FedEx 2 Day"=>"$20.59", "FedEx Standard Overnight"=>"$33.78", "USPS Priority Mail 1-Day"=>"$5.60"}
What's happening is that ruby is printing to the console the expected output, and then returning the hash itself as the return value.
So you should see the output that you expected in your rails logs. However, the html being displayed is the return value of your each statement, which is just the hash itself.
What you want to do is something like this:
<ul>
<% ship_hash.each do |key, value| %>
<li><%= key %>: <%= value %></li>
<% end %>
</ul>
This will output the key value pairs of the hash in the form of an unordered list.
In my index.js.erb
$(".div").html("<%= escape_javascript(render('something/some_form')) %>");
in some_form.htm.erb i have condition which is not working
<% if #variable1 == #variable2 %>
show something1
<% else %>
show something2
<% end %>
For some reason when my js.erb renders this partial i always have show something2. So <% if #variable1 == #variable2 %> condition not working
variable1 and variable2 defined in my controller
How can i make this equal condition work?
Controller variables are not visible within partials. Please, read the official documentation on partials
You need to pass those variables along to the partial via the locals:
//e.g locals: {variable1: #variable1, variable2: #variable2}
$(".div").html("<%= escape_javascript(render('something/some_form', locals: {variable1: #variable1, variable2: #variable2})) %>");
some_form
<% if variable1 == variable2 %>
show something1
<% else %>
show something2
<% end %>
I have an app where users can sign up with Facebook, in which case I take the Facebook image, or they can sign up using regular authentication, in which case they're uploading a photo using CarrierWave gem.
Because of some sort of conflict, I had to create a column in the database (:image)to store the url to the Facebook image, and then a different column (:dimage) to store the url for the image if they signed up using the non-facebook sign up.
So when it comes time to display the user, I'm checking to see if there's an :image, and, if not, then displaying the other :dimage.
However, I don't want to require them to upload an image, in which case, I want to display an anon image (here represented by the rails.png). However, the Rails.png isn't showing up (just a broken image path), so I'm assuming there's some sort of error with my if else syntax because the image_tag("rails.png") is taken straight from the api
<% if user.image %>
<%= image_tag user.image %>
<% elsif user.dimage %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
The generated html on the rails.png
<img alt="Assets" src="/assets/">
Use the present? method to check if the attribute is not empty:
<% if user.image.present? %>
<%= image_tag user.image %>
<% elsif user.dimage.present? %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
You'll save yourself a headache or two because in Ruby the only falsehoods are false and nil. This means that the empty string "" will actually return true.
You need to look at look into user.image.nil? user.image.empty? and user.image.blank? depending on what you're using and apply the same to the rest of your conditional. You need to find out specifically what your looking for or the if else won't give you what you want. I would try empty first which checks if something is nil or is an empty string.
When you create a new rails application, rails stores its logo in PROJECT_PATH/app/assets/images folder. Could you check if that image exists?
I am coding a web application in ruby on rails.
I have a set of text boxes in each one there is a character and i want to glue all these text boxes in order to make one word.
The text boxes are like this :
1 %>
any ideas ??
Well for starters dont use a for loop, they ugly.
Second I wouldn't use the text_field helper rather the text_field tag
<% (1..10).each do |n| %>
<%= text_field_tag "password[#{n}]" %>
<% end %>
That will return the password all nicely chunked up