Help with RJS please

0 views
Skip to first unread message

Jason Patrick Agujo

unread,
Dec 7, 2009, 9:20:47 AM12/7/09
to ruby...@googlegroups.com
what i want to do is make the company field show up only when the role selected is 'Client'

here are the codes that i have:
VIEW
<p>
    <label>Role</label><br/>
    <%= select :role, :role, Role.all.collect{|r| [r.name,r.name]}, :include_blank => true %>
</p>

<%= observe_field "role_role",{ :function=>"just_test" }%>

<p id="client_id_space">
    <label for="client_id">Clients Account</label><br/>
    <%= f.select :client_id, Client.all.collect{|r| [r.company_name,r.id]},  :include_blank => true %>
</p>

<script type="text/javascript">
    Element.hide('client_id_space');
   
    function just_test(p_role){
        page.alert('Inside function!');
    }
</script>

right now im stuck in trying to pass the data to go inside the defines JS "just_test". but when i move the alert to the observe_field under position it works. can you guys point me in the right direction please.

thank you for taking the time to read this.

Evan Sagge

unread,
Dec 7, 2009, 10:09:37 AM12/7/09
to ruby...@googlegroups.com
This line: <%= observe_field "role_role",{ :function=>"just_test" }%>

will actually generate the following javascript:

new Form.Element.Observer('role_role', 1, function(element, value)
{ just_test }) // JS syntax error!

I think you wanted to do this instead

<%= observe_field "role_role",{ :function=>"just_test(value)" }%>

so it will generate this:

new Form.Element.Observer('role_role', 1, function(element, value)
{ just_test(value) })

The observe_field method wraps the JS code you specify in :function in
its own function(element, value) {}, with the same parameter names.
> --
>
> You received this message because you are subscribed to the Google
> Groups "Philippine Ruby Users Group (PRUG/PhRUG)" group.
> To post to this group, send email to ruby...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-phil+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/ruby-phil?hl=en
> .

Jason Patrick Agujo

unread,
Dec 7, 2009, 10:26:20 AM12/7/09
to ruby...@googlegroups.com
i got to try that also, this is what i ended up with:

<%= observe_field("role_role",{:function=>"just_test($('role_role').value)"}) %>

im able to get to get the value of the selected data from roel through:

<%= observe_field("role_role",{:function=>"alert($('role_role').value)"}) %>

but when i replace 'alert' with 'just_test' it wont even do the:

page.alert('inside function!');

im not sure if im breaking a rule or what but its not giving that alert. if that alert shows up then that means im inside the actual JS that i made.

Evan Sagge

unread,
Dec 7, 2009, 10:38:00 AM12/7/09
to ruby...@googlegroups.com
Like I mentioned, if you just put "just_test", you will get: function(element, value){ just_test }

You would want it to result to: function(element, value){ just_test(value) }, so use this instead:

<%= observe_field("role_role",{:function=>"just_test(value)"}) %>

Reply all
Reply to author
Forward
0 new messages