Forum: Ruby on Rails view code regular expression

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-15 17:37
(Received via mailing list)
I'm lost on regular expressions to begin with...

I'm trying to fix a value to one of 4 radio buttons as there will be
value of either 1,2,3 or 4 in @roles_users...

  <TD><input type="radio" id="roles"
    name="case_managers[case_manager_name]"
    value="Case Manager Admin"
    <% if =~ @roles_users /1/ checked = "checked" %> /></TD>
  <TD><input type="radio" id="roles"
    name="case_managers[case_manager_name]"
    value="Case Manager Create"
    <% if =~ @roles_users /2/ checked = "checked" %> /></TD>
  <TD><input type="radio" id="roles"
    name="case_managers[case_manager_name]"
    value="Case Manager View"
    <% if =~ @roles_users /3/ checked = "checked" %> /></TD>
  <TD><input type="radio" id="roles"
    name="case_managers[case_manager_name]"
    value="Case Manager None"
    <% if =~ @roles_users /4/ checked = "checked" %> /></TD>

but this code gets me a syntax error...

@roles_users is a hash created from a find.

Craig
B7aa6f0356566cc801f769c6fc14ba1a?d=identicon&s=25 Francois GORET (Guest)
on 2006-04-15 17:50
(Received via mailing list)
On Saturday 15 April 2006 22:34, Craig White wrote:
> I'm lost on regular expressions to begin with...
>
> I'm trying to fix a value to one of 4 radio buttons as there will be
> value of either 1,2,3 or 4 in @roles_users...

So @roles_users is an integer, or a string ? I assume an integer...

>   <TD><input type="radio" id="roles"
>     name="case_managers[case_manager_name]"
>     value="Case Manager Admin"
>     <% if =~ @roles_users /1/ checked = "checked" %> /></TD>

<%= "checked" if @roles_users == 1 %>

Note that the 'if' must have a corresponding 'end'. The one line form of
'if'
has a mandatory 'then' too:
if @roles_users == 1 then "checked" end ... is the verbose equivalent of
the
expression above.

The syntax for a regular expression is @roles_users =~ /1/


>     value="Case Manager None"
>     <% if =~ @roles_users /4/ checked = "checked" %> /></TD>
>
> but this code gets me a syntax error...
>
> @roles_users is a hash created from a find.

Hmm... so it's not an integer / string like I understood from the
beginning?
What's the contents of your hash?

Francois
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-15 17:59
(Received via mailing list)
On Sat, 2006-04-15 at 22:47 +0700, Francois GORET wrote:
> >     value="Case Manager Admin"
>
> >     name="case_managers[case_manager_name]"
> >     value="Case Manager None"
> >     <% if =~ @roles_users /4/ checked = "checked" %> /></TD>
> >
> > but this code gets me a syntax error...
> >
> > @roles_users is a hash created from a find.
>
> Hmm... so it's not an integer / string like I understood from the beginning?
> What's the contents of your hash?
----
@roles_users.inspect says...
[#"3", "user_id"=>"18"}>, #"7", "user_id"=>"18"}>, #"11",
"user_id"=>"18"}>, #"15", "user_id"=>"18"}>, #"19", "user_id"=>"18"}>]

Craig
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-15 18:24
(Received via mailing list)
Francois GORET wrote:
> <%= "checked" if @roles_users == 1 %>
>
> Note that the 'if' must have a corresponding 'end'.
Not so:

alex@pandora:~$ irb
irb(main):001:0> puts 'foo' if 1 == 1
foo
=> nil
irb(main):002:0> puts 'foo' if 1 == 0
=> nil
irb(main):003:0>

I don't think I've ever tried that syntax in ERb, though, so I can't
vouch for it in a template...
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-16 00:05
(Received via mailing list)
On Sat, 2006-04-15 at 17:21 +0100, Alex Young wrote:
> irb(main):002:0> puts 'foo' if 1 == 0
> => nil
> irb(main):003:0>
>
> I don't think I've ever tried that syntax in ERb, though, so I can't
> vouch for it in a template...
----
still I'm struggling with this...in script/console

>> @roles_users
=> [#<RolesUsers:0xb796f490 @attributes={"role_id"=>"3",
"user_id"=>"18"}>, #<RolesUsers:0xb796f454 @attributes={"role_id"=>"7",
"user_id"=>"18"}>, #<RolesUsers:0xb796f404 @attributes={"role_id"=>"11",
"user_id"=>"18"}>, #<RolesUsers:0xb796f2c4 @attributes={"role_id"=>"15",
"user_id"=>"18"}>, #<RolesUsers:0xb796f274 @attributes={"role_id"=>"19",
"user_id"=>"18"}>]

I'm trying to figure out if there's a 'role_id' that equals 1, 2, 3 or 4
from the above...

>> 3 =~ /@roles_users/
=> false
>> "3" =~ /@roles_users/
=> nil
>> ':role_id=>"3"' =~ /@roles_users/
=> nil
>> 'role_id=>"3"' =~ /@roles_users/
=> nil
>> 'role_id=>3' =~ /@roles_users/
=> nil

If I assume which element, I can get it but I can't just assume that
(well I can if I 'order' the items when I retrieve them) but I sure
would love to know how to use regex to parse @roles_users anyway.

Craig
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-16 00:39
(Received via mailing list)
Hi --

On Sat, 15 Apr 2006, Craig White wrote:

>> => nil
> => [#<RolesUsers:0xb796f490 @attributes={"role_id"=>"3",
> => false
>>> "3" =~ /@roles_users/
> => nil
>>> ':role_id=>"3"' =~ /@roles_users/
> => nil
>>> 'role_id=>"3"' =~ /@roles_users/
> => nil
>>> 'role_id=>3' =~ /@roles_users/
> => nil

You're performing various tests on the string "@roles_users", which
you can tell at a glance doesn't contain a "3" :-)

> If I assume which element, I can get it but I can't just assume that
> (well I can if I 'order' the items when I retrieve them) but I sure
> would love to know how to use regex to parse @roles_users anyway.

@roles_users is an array, and regexes are for strings, so it's not the
right way.  Here's a way to see whether the array contains an element
whose role_id is "1" through "4":

   @roles_users.any? {|ru| %w{1 2 3 4}.include?(ru.role_id) }


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-17 17:17
(Received via mailing list)
On Sat, 2006-04-15 at 18:36 -0400, dblack@wobblini.net wrote:
> >>
> > ----
> > I'm trying to figure out if there's a 'role_id' that equals 1, 2, 3 or 4
> >>> 'role_id=>3' =~ /@roles_users/
> right way.  Here's a way to see whether the array contains an element
> whose role_id is "1" through "4":
>
>    @roles_users.any? {|ru| %w{1 2 3 4}.include?(ru.role_id) }
----
thanks - that was exactly what I need as I am likely to re-factor my
code because I really don't like 'assuming' the data isn't going to
change, even though I am in control over the data in this case. I
recognize my propensity for shooting myself in the foot and this section
of code bothered me and what you pointed out appears to be a much better
solution, but I had to move on at the time and thus, this information
will wait a few days as I now have things working and will do some
cleanup over time.

Thanks

Craig
This topic is locked and can not be replied to.