RJS and Rails

Ok this is the code I have in my view:


- Select -
Male
Female

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

hi, you do have a div id called “client_gender” right?, also your =
needs to be == right?

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”

if @client_gender.value = ‘Male’

if @client_gender.value == ‘Male’?

in your action.

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.

On 6/13/07, Matthew L. [email protected] wrote:

Ok this is the code I have in my view:


- Select -
Male
Female

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 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 - Male Female <% = observe_field "cliend_gender", {:url => {:action => change}, :on => "changed"}

This should take the “value” of the tag and pass it in the
params hash

On 13 Jun., 17:00, Matthew L. [email protected]

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 }

Matthew L. wrote:

Ok this is the code I have in my view:


- Select -
Male
Female

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]

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 - Male Female <% = observe_field "cliend_gender", {:url => {:action => change}, :on => "changed"}

This should take the “value” of the tag and pass it in the
params hash

On 13 Jun., 17:00, Matthew L. [email protected]

How did you get your @client_gender.value ?? you should just set
@client_gender = params[:client][:gender]

No thats my mistake, I want the id which is “client_gender” so
instead I should use page[‘client_gender’].value == “Male”