ruby script if statement

35 views
Skip to first unread message

yinwen Xuan

unread,
Dec 2, 2013, 3:24:17 PM12/2/13
to rubyonra...@googlegroups.com
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!

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Dec 2, 2013, 3:42:27 PM12/2/13
to rubyonra...@googlegroups.com
On 2 December 2013 20:24, yinwen Xuan <li...@ruby-forum.com> 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

yinwen Xuan

unread,
Dec 2, 2013, 3:50:41 PM12/2/13
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Dec 2, 2013, 3:56:03 PM12/2/13
to rubyonra...@googlegroups.com
On 2 December 2013 20:50, yinwen Xuan <li...@ruby-forum.com> 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

yinwen Xuan

unread,
Dec 2, 2013, 4:05:33 PM12/2/13
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Dec 2, 2013, 4:13:02 PM12/2/13
to rubyonra...@googlegroups.com
On 2 December 2013 21:05, yinwen Xuan <li...@ruby-forum.com> 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

yinwen Xuan

unread,
Dec 2, 2013, 4:22:44 PM12/2/13
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Dec 2, 2013, 4:52:49 PM12/2/13
to rubyonra...@googlegroups.com
On 2 December 2013 21:22, yinwen Xuan <li...@ruby-forum.com> 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 &sup2; perhaps.

What are the variables M2 and H2 set to?

Colin

yinwen Xuan

unread,
Dec 2, 2013, 5:16:11 PM12/2/13
to rubyonra...@googlegroups.com
Colin Law wrote in post #1129260:
> On 2 December 2013 21:22, yinwen Xuan <li...@ruby-forum.com> 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 &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!

Walter Lee Davis

unread,
Dec 2, 2013, 6:58:51 PM12/2/13
to rubyonra...@googlegroups.com
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

yinwen Xuan

unread,
Dec 2, 2013, 8:29:00 PM12/2/13
to rubyonra...@googlegroups.com
Walter Davis 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

yinwen Xuan

unread,
Dec 2, 2013, 8:33:17 PM12/2/13
to rubyonra...@googlegroups.com
Walter Davis 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&sup2; <%
else %> <% end %>

but not working

Walter Lee Davis

unread,
Dec 2, 2013, 9:39:01 PM12/2/13
to rubyonra...@googlegroups.com
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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f357ff60cfa96f4849285867f74dac7e%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.

yinwen Xuan

unread,
Dec 2, 2013, 10:03:17 PM12/2/13
to rubyonra...@googlegroups.com
Walter Davis 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&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 :-(
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 %>

Scott Ribe

unread,
Dec 2, 2013, 10:17:28 PM12/2/13
to rubyonra...@googlegroups.com
On Dec 2, 2013, at 8:03 PM, yinwen Xuan <li...@ruby-forum.com> wrote:

> Then I extend your code to the follow, but not working :-(

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" ;-)


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




yinwen Xuan

unread,
Dec 2, 2013, 10:43:20 PM12/2/13
to rubyonra...@googlegroups.com
>
> 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.

Scott Ribe

unread,
Dec 2, 2013, 11:03:23 PM12/2/13
to rubyonra...@googlegroups.com
On Dec 2, 2013, at 8:43 PM, yinwen Xuan <li...@ruby-forum.com> 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.

yinwen Xuan

unread,
Dec 3, 2013, 8:08:57 PM12/3/13
to rubyonra...@googlegroups.com
Scott Ribe wrote in post #1129304:
> On Dec 2, 2013, at 8:43 PM, yinwen Xuan <li...@ruby-forum.com> 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 Ribe

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

Scott Ribe

unread,
Dec 3, 2013, 8:30:33 PM12/3/13
to rubyonra...@googlegroups.com
On Dec 3, 2013, at 6:08 PM, yinwen Xuan <li...@ruby-forum.com> wrote:

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

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.

yinwen Xuan

unread,
Dec 5, 2013, 4:56:11 PM12/5/13
to rubyonra...@googlegroups.com
Scott Ribe wrote in post #1129470:
> On Dec 3, 2013, at 6:08 PM, yinwen Xuan <li...@ruby-forum.com> wrote:
>
>> Sorry Scott, All fixed now.
>> you are a good teacher!!
>> let me know if you want student, I am happy to pay tuition ;-)
>
> 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?

Joel Pearson

unread,
Dec 5, 2013, 6:22:30 PM12/5/13
to rubyonra...@googlegroups.com
Rather than asking for small pieces of information one at a time on a
forum, you would be much better served by completing a few Rails
tutorials and creating some Rails projects on your own computer. Then
you will understand what's happening on the server.
Reply all
Reply to author
Forward
0 new messages