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!

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

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

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

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

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

Thanks!

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 &sup2; perhaps.

What are the variables M2 and H2 set to?

Colin

Colin Law wrote in post #1129260:

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 &sup2; 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&sup2; is html to display m² , then I change it to:

<%= land_area -%><%= if land_area_unit == M2 %> m&sup2; <% 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!

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 Davis wrote in post #1129271:

Walter Davis wrote in post #1129271:

Walter Davis wrote in post #1129271:

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&sup2; <% else %> <% end %>

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&sup2;<%- else %><%= land_area_unit %><%- end %>

Which could arguably be written a lot more neatly:

Land Area: <%= "#{land_area} #{(land_area_unit == 'M2')? 'm&sup2;' : land_area_unit}".html_safe %>

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

Walter

Walter Davis wrote in post #1129287:

%> 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&sup2;<%- else %><%= land_area_unit %><%- end %>

Which could arguably be written a lot more neatly:

Land Area: <%= "#{land_area} #{(land_area_unit == 'M2')? 'm&sup2;' : 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&sup2; <%- else %><%- if land_area_unit == 'HA' %>Ha <%- else %><%- if land_area_unit == 'AC' %>Ac <%- else %>f&sup2;<%- end %>

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:

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&sup2; <%- 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.

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

Scott Ribe wrote in post #1129304:

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 Ribe wrote in post #1129470: