0

I have a select input on my page. this select input displays/hides fields in the form. This all works fine. But the problem is that if i submit the form and lack some necessary fields, it doesnt set the select to the right value afterwards. I just cant get the embedded ruby to work! it keeps escaping the whole thing... here my code:

$(document).ready(function() {
$("#profile_sex").val('<%= @profile.sex %>')
   $("#profile_sex").change(function(){



    ($(this).val() == "Frau") ? $('#form-female').show() : $('#form-female').hide();
    ($(this).val() == "Mann") ? $('#form-male').show() : $('#form-male').hide();
    if ($(this).val() == "Paar") {
        $('#form-female').show(); 
        $('#form-male').show();
    }       
  });

});

why doesnt this work??? I dont get any error or anything it just sets the value to "<%= @profile.sex =>" I was googling and searching about on stack overflow and railscasts, the rails API, everything. Im seriously confused...

thanks for your help.

3 Answers 3

1

Your JS script doesn't execute embedded ruby code. Here is a description how to resolve this:

http://bitprison.net/rails3_dynamic_javascript

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the tick marks around '<%= @profile.sex %>'.

If you need the tick marks around your value, try ' + <%= @profile.sex %> + '

1 Comment

Even better, use @profile.sex.inspect or @profile.sex.to_json to prevent any stray quotation marks from breaking your script.
0

You may want to take a look at jsvars. I am using it in one of my projects and it's pretty great. You can set your javascript variables right in the controller by assigning to a hash:

jsvars[:profile_sex] = @profile.sex

then you could do $("#profile_sex").val(profile_sex); in your script. It might solve your problem and make things prettier at the same time.

Just a thought!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.