1

I have a very long operation that ends in a huge array. That array is passed as a Ruby object to a js.erb file which renders a partial using it:

$('#xxx').html("<%= escape_javascript(render('part', testVar: @fromServer)) %>");

This all goes well, but I'd like to be able to sort columns in the resulting table without having to re-do the entire operation. It should be easy, but I can't get the resulting variable in the partial to persist. My goal is to be able to have a script tag in the partial:

<script type="text/javascript">
    $("#column").click(function(){
        $.ajax({
              type : 'POST',
              url : '/sort_column',
              data : {data: jsVar},
              dataType : 'script'
        });
    });
</script>

How would I go about doing this?

1 Answer 1

1

Just wrap what you need from ruby in <%= %>. Like that:

data : {data: "<%= testVar %>"},

Since document will be send to ruby interpreter it will contain your testVar value. Also please use snake case notation instead of camel case. It's common agreement in ruby.

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

2 Comments

For whatever reason, the script just isn't being rendered. Is there some rule in Ruby about rendering scripts in partials?
If you don't getting errors then probably your controller logic works not as expected and not responding with js, could you add controller method to the question?

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.