Forum: Ruby on Rails RJS and Rails

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.
Matthew L. (Guest)
on 2007-06-13 17:43
Ok this is the code I have in my view:

<select id="client_gender" name="client[gender]" onChange="<%=
remote_function(
:url => {:action => 'change'}) %>">
        <option value="">- Select -</option>
        <option value="Male">Male</option>
        <option value="Female">Female</option>
</select>

now in my change.rjs, I have:

if @client_gender.value = 'Male'
  page.remove 'client_gender'
  page.show 'male_form'
else
  page.remove 'client_gender'
  page.show 'female_form'
end

When I click on the option I want in my dropdown, it always shows my
'male_form' no matter what I pick in the dropdown.

How can I fix this? And what If I had like 3 or more options what would
I do?

Thanks
mixplate (Guest)
on 2007-06-13 18:17
hi, you do have a div id called "client_gender" right?, also your =
needs to be == right?
Matthew L. (Guest)
on 2007-06-13 18:21
mixplate wrote:
> hi, you do have a div id called "client_gender" right?, also your =
> needs to be == right?

no my select id="client_gender", and I tried with the == and it didn't
do anything, and Firebug didn't give me any errors.

I have a div id="male_form" and a dive id="female_form"
mixplate (Guest)
on 2007-06-13 18:51
if @client_gender.value = 'Male'

if @client_gender.value == 'Male'?


in your action.
Matthew L. (Guest)
on 2007-06-13 19:00
mixplate wrote:
> if @client_gender.value = 'Male'
>
> if @client_gender.value == 'Male'?
>
>
> in your action.

I've tried that and nothing happens when I choose an option, I don't
even get any errors either.
Bob S. (Guest)
on 2007-06-13 19:44
(Received via mailing list)
On 6/13/07, Matthew L. <removed_email_address@domain.invalid> wrote:
>
> Ok this is the code I have in my view:
>
> <select id="client_gender" name="client[gender]" onChange="<%=
> remote_function(
> :url => {:action => 'change'}) %>">
>         <option value="">- Select -</option>
>         <option value="Male">Male</option>
>         <option value="Female">Female</option>
> </select>

You have nothing in your remote_function to pass the selected value to
the action. You're using Firebug, so turn on "ShowXMLHttpRequests" so
you can see the exact request being  sent to the server. You'll see
that "Male" or "Female" are not being sent.

You should:

   1) Wrap the whole thing in a remote_form_for() call.
   2) Use observe_field() to submit the form on the 'change' event for
'client_gender'

>
> now in my change.rjs, I have:
>
> if @client_gender.value = 'Male'

This is always true, because you're assigning 'Male' to the variable.
You want == for an equality test. At that point, it will always be
false until you fix the problem above.
Thorsten (Guest)
on 2007-06-13 19:47
(Received via mailing list)
Well i guess you use the wrong helper here. Remote-function doesn't
automacigally take the value of the selected option and sends it in
params[] i think. You took the code from the Rails API docs i assume.
Your controller code might be helpful too

You should try observe_field:

<select id="client_gender" name="client[gender]">
        <option value="">- Select -</option>
        <option value="Male">Male</option>
        <option value="Female">Female</option>
</select>
<% = observe_field "cliend_gender", {:url => {:action => change}, :on
=> "changed"}

This should take the "value" of the <select> tag and pass it in the
params hash

On 13 Jun., 17:00, Matthew L. <removed_email_address@domain.invalid>
Matthew L. (Guest)
on 2007-06-13 20:23
Thorsten wrote:
> Well i guess you use the wrong helper here. Remote-function doesn't
> automacigally take the value of the selected option and sends it in
> params[] i think. You took the code from the Rails API docs i assume.
> Your controller code might be helpful too
>
> You should try observe_field:
>
> <select id="client_gender" name="client[gender]">
>         <option value="">- Select -</option>
>         <option value="Male">Male</option>
>         <option value="Female">Female</option>
> </select>
> <% = observe_field "cliend_gender", {:url => {:action => change}, :on
> => "changed"}
>
> This should take the "value" of the <select> tag and pass it in the
> params hash
>
> On 13 Jun., 17:00, Matthew L. <removed_email_address@domain.invalid>

Ok i've tried it with the observe_field and when I select an option and
nothing still happens. This is what I get as a response in my firebug
console:

try {

$("client_gender").value().= = "Male";

Element.hide("client_gender");

Element.show("male_form");

} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('$(\"client_gender\").value().= = \"Male

\";\nElement.hide(\"client_gender\");\nElement.show(\"male_form\");');
throw e }
Francis S. (Guest)
on 2007-06-13 21:48
Matthew L. wrote:
> Ok this is the code I have in my view:
>
> <select id="client_gender" name="client[gender]" onChange="<%=
> remote_function(
> :url => {:action => 'change'}) %>">
>         <option value="">- Select -</option>
>         <option value="Male">Male</option>
>         <option value="Female">Female</option>
> </select>
> end
>
> When I click on the option I want in my dropdown, it always shows my
> 'male_form' no matter what I pick in the dropdown.
>
> How can I fix this? And what If I had like 3 or more options what would
> I do?
>
> Thanks

How did you get your @client_gender.value ?? you should just set
@client_gender = params[:client][:gender]
Matthew L. (Guest)
on 2007-06-13 21:53
> How did you get your @client_gender.value ?? you should just set
> @client_gender = params[:client][:gender]

No thats my mistake, I want the <select> id which is "client_gender" so
instead I should use page['client_gender'].value == "Male"
This topic is locked and can not be replied to.