Ruby script if statement

Can anyone have a look this ruby script why it’s not working:

<%= if land_area_unit == M2 %>m&sup2 <% else %> <%= if land_area_unit
== HA %>Ha <% else %><%= if land_area_unit == AC %>Ac<% else %>f&sup2

What I want to do is show the right unit for the land area.

Thanks!

Hi Colin, Thanks for your advice,
but I can’t access to the rails server, only can edit the ruby script.
any chance you can check anything wrong with the If statement script?

cheers
Wayne

On 2 December 2013 20:50, yinwen Xuan [email protected] wrote:

Hi Colin, Thanks for your advice,
but I can’t access to the rails server, only can edit the ruby script.
any chance you can check anything wrong with the If statement script?

What do you mean you only have access to the ruby script? The script
you showed is in the view, which runs on the server. I am suggesting
that you change it in the controller ruby script instead of the view
ruby script.

Colin

On 2 December 2013 20:24, yinwen Xuan [email protected] wrote:

Can anyone have a look this ruby script why it’s not working:

<%= if land_area_unit == M2 %>m&sup2 <% else %> <%= if land_area_unit
== HA %>Ha <% else %><%= if land_area_unit == AC %>Ac<% else %>f&sup2

It may be better to do it in the controller anyway, keep the view as
simple as possible. In the controller setup @units as appropriate and
then just display it in the view. Logic in the view is best avoided
wherever possible.

Colin

I mean I can’t edit the server, like view controller.
I only can edit the html themes, check the attachment for the screen
shot
sorry, I am a beginner and just want do some maintaining
cheers

On 2 December 2013 21:05, yinwen Xuan [email protected] wrote:

Please don’t top post, and please quote the previous message.
Remember this is a mailing list, not a forum, though you may be
accessing it through a forum like interface.

I mean I can’t edit the server, like view controller.
I only can edit the html themes, check the attachment for the screen
shot
sorry, I am a beginner and just want do some maintaining
cheers

Attachments:
http://www.ruby-forum.com/attachment/8998/test.jpg

Sorry, I have no idea what theme you are trying to adjust. Presumably
you are using some application with configurable themes. I think you
had better give us some more information about your environment.

Colin

could you just check why the ruby script not work

it work when only use the field name : <%= land_area_unit -%>

because the the field display not right, when its sqm, it show M2 I want
it show m², that’s why I want to write a if statement to showing the
right unit format:

<%= if land_area_unit == M2 %>m&sup2 <% else %> <%= if land_area_unit
== HA %>Ha <% else %><%= if land_area_unit == AC %>Ac<% else %>f&sup2

but this is not work for some reason.

Thanks!

On 2 December 2013 21:22, yinwen Xuan [email protected] wrote:

could you just check why the ruby script not work

Why should I help when you have not followed my request not to top
post and to remember to quote the previous reply?

it work when only use the field name : <%= land_area_unit -%>

because the the field display not right, when its sqm, it show M2 I want
it show m, that’s why I want to write a if statement to showing the
right unit format:

<%= if land_area_unit == M2 %>m&sup2 <% else %> <%= if land_area_unit
== HA %>Ha <% else %><%= if land_area_unit == AC %>Ac<% else %>f&sup2

There are so many things wrong I don’t know where to start. Have a
look at
http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/.

What is &sup2 intended to be? Should it be ² perhaps.

What are the variables M2 and H2 set to?

Colin

On Dec 2, 2013, at 5:16 PM, yinwen Xuan wrote:

because the the field display not right, when its sqm, it show M2 I want

the original code on the themes is:
and thanks in advance!
If you’re trying to do a comparison, you need to quote the M2, since it
will be a string value, not a constant. Also, if you’re doing a
comparison (which you don’t want to output any value) then don’t use <%=
%> as the container, rather <% %> or <%- %> so as to avoid outputting
the result of land_area_unit == ‘M2’, which would be ‘true’ some of the
time.

Walter

Walter D. wrote in post #1129271:

On Dec 2, 2013, at 5:16 PM, yinwen Xuan wrote:

because the the field display not right, when its sqm, it show M2 I want

the original code on the themes is:
and thanks in advance!
If you’re trying to do a comparison, you need to quote the M2, since it
will be a string value, not a constant. Also, if you’re doing a
comparison (which you don’t want to output any value) then don’t use <%=
%> as the container, rather <% %> or <%- %> so as to avoid outputting
the result of land_area_unit == ‘M2’, which would be ‘true’ some of the
time.

Walter

hi Walter, Thanks for your advcie, so what should I change it to, I
just want to display m² instead of M2.
cheers

Colin L. wrote in post #1129260:

On 2 December 2013 21:22, yinwen Xuan [email protected] wrote:

could you just check why the ruby script not work

Why should I help when you have not followed my request not to top
post and to remember to quote the previous reply?

it work when only use the field name : <%= land_area_unit -%>

because the the field display not right, when its sqm, it show M2 I want
it show m, that’s why I want to write a if statement to showing the
right unit format:

<%= if land_area_unit == M2 %>m&sup2 <% else %> <%= if land_area_unit
== HA %>Ha <% else %><%= if land_area_unit == AC %>Ac<% else %>f&sup2

There are so many things wrong I don’t know where to start. Have a
look at
http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/.

What is &sup2 intended to be? Should it be ² perhaps.

What are the variables M2 and H2 set to?

Colin

Hi Colin,

I do want to do what you request, but as I explain that I can’t edit the
server, view controller etc
the original code on the themes is:
<%= land_area -%><%= land_area_unit -%>
it show on website is 1234 M2
what I want to do is change the M2 to m²
m² is html to display m² , then I change it to:

<%= land_area -%><%= if land_area_unit == M2 %> m² <% else %>Ha

Ha is Hectares
but its not work, sorry that I can’t do too much following what you
request because I don’t know how.
and thanks in advance!

Walter D. wrote in post #1129271:

On Dec 2, 2013, at 5:16 PM, yinwen Xuan wrote:

because the the field display not right, when its sqm, it show M2 I want

the original code on the themes is:
and thanks in advance!
If you’re trying to do a comparison, you need to quote the M2, since it
will be a string value, not a constant. Also, if you’re doing a
comparison (which you don’t want to output any value) then don’t use <%=
%> as the container, rather <% %> or <%- %> so as to avoid outputting
the result of land_area_unit == ‘M2’, which would be ‘true’ some of the
time.

Walter

I tried this:

Land Area: <%= land_area %> <%= if land_area_unit == M2 %> m² <%
else %> <% end %>

but not working

Walter D. wrote in post #1129287:

On Dec 2, 2013, at 8:33 PM, yinwen Xuan wrote:

%> as the container, rather <% %> or <%- %> so as to avoid outputting
but not working
This is the same code I commented on, I don’t see that you took up any
of my suggestions here.

Land Area: <%= land_area %> <%- if land_area_unit == ‘M2’ %>m²<%-
else %><%= land_area_unit %><%- end %>

Which could arguably be written a lot more neatly:

Land Area: <%= “#{land_area} #{(land_area_unit == ‘M2’)? ‘m²’ :
land_area_unit}”.html_safe %>

Still quite a mess to have in your view, though.

Walter

Hi Walter, your first script is working!! that’s great!!!

since the land unit got three others:
AC - Acres
HA - Hectares
F2 - Sq feet

Then I extend your code to the follow, but not working :frowning:
Land Area: <%= land_area %>
<%- if land_area_unit == ‘M2’ %>m²
<%- else %><%- if land_area_unit == ‘HA’ %>Ha
<%- else %><%- if land_area_unit == ‘AC’ %>Ac
<%- else %>f²<%- end %>

On Dec 2, 2013, at 8:03 PM, yinwen Xuan [email protected] wrote:

Then I extend your code to the follow, but not working :frowning:

How many "if"s? How many "end"s? See the problem?

Seriously, you need to work through a tutorial, as suggested earlier,
rather than expect the members of this forum to write your code one
statement at a time.

And when you take the time to work through a tutorial, you’ll learn
about “elsif” :wink:


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

On Dec 2, 2013, at 8:33 PM, yinwen Xuan wrote:

%> as the container, rather <% %> or <%- %> so as to avoid outputting
but not working
This is the same code I commented on, I don’t see that you took up any
of my suggestions here.

Land Area: <%= land_area %> <%- if land_area_unit == ‘M2’ %>m²<%-
else %><%= land_area_unit %><%- end %>

Which could arguably be written a lot more neatly:

Land Area: <%= “#{land_area} #{(land_area_unit == ‘M2’)? ‘m²’ :
land_area_unit}”.html_safe %>

Still quite a mess to have in your view, though.

Walter

On Dec 2, 2013, at 8:43 PM, yinwen Xuan [email protected] wrote:

but the result is all showing sq feet, any idea, sorry I don’t know
where else I can work it out.

You’re very close, but spelling matters. Review my prior message to you.


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

How many "if"s? How many "end"s? See the problem?

Seriously, you need to work through a tutorial, as suggested earlier,
rather than expect the members of this forum to write your code one
statement at a time.

Sorry and thanks! I did go through the tutorial and edit my code as
this:

Land Area: <%= land_area %>
<%- if land_area_unit == ‘M2’ %>m²
<%- elseif land_area_unit == ‘HA’ %>Ha
<%- elseif land_area_unit == ‘AC’ %>Ac
<%- else %>sq feet<%- end %>

but the result is all showing sq feet, any idea, sorry I don’t know
where else I can work it out.

Scott R. wrote in post #1129304:

On Dec 2, 2013, at 8:43 PM, yinwen Xuan [email protected] wrote:

but the result is all showing sq feet, any idea, sorry I don’t know
where else I can work it out.

You’re very close, but spelling matters. Review my prior message to you.


Scott R.

Sorry Scott, All fixed now.
you are a good teacher!!
let me know if you want student, I am happy to pay tuition :wink:

Scott R. wrote in post #1129470:

On Dec 3, 2013, at 6:08 PM, yinwen Xuan removed_e[email protected] wrote:

Sorry Scott, All fixed now.
you are a good teacher!!
let me know if you want student, I am happy to pay tuition :wink:

I’m glad it’s working.

One last thing, regarding people who told you to move this from view to
controller, and your response that you have no access to the files on
the server. This file that you are editing is on the server. It is in
the same folder hierarchy as the controller. It is very unlikely you
have access to this RoR view file but not the RoR controllers or models.

I actually not quite understand how it set up, it was build by some one
else.
I can only log in to rails server as administrator and edit and create
themes using html and ruby script, I also can create and edit the user,
then add them to different themes

can you teach me more?

On Dec 3, 2013, at 6:08 PM, yinwen Xuan [email protected] wrote:

Sorry Scott, All fixed now.
you are a good teacher!!
let me know if you want student, I am happy to pay tuition :wink:

I’m glad it’s working.

One last thing, regarding people who told you to move this from view to
controller, and your response that you have no access to the files on
the server. This file that you are editing is on the server. It is in
the same folder hierarchy as the controller. It is very unlikely you
have access to this RoR view file but not the RoR controllers or models.


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs