RJS and Rails


#1

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


#2

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


#3

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”


#4

if @client_gender.value = ‘Male’

if @client_gender.value == ‘Male’?

in your action.


#5

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.


#6

On 6/13/07, Matthew L. removed_email_address@domain.invalid 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.


#7

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. 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 }


#8

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]


#9

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. removed_email_address@domain.invalid


#10

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”