Lee
Lee

Reputation: 20984

Javascript inside view in rails 3

I need to put some javascript inside a view. Basically I am having a play with the Recurly.js library.

Here is an example form: http://pastie.org/3142536

I have tried adding the JS to a partial: filename _recurly.js.erb

<%= render :partial => 'recurly.js', :locals => { :company => @company } %>

But its just outputting the JS to the page.

Is there a better way of doing this passing vars to JS for output and how can I get it to render the JS ?

Hope you can advise.

Upvotes: 0

Views: 216

Answers (3)

Manish Shrivastava
Manish Shrivastava

Reputation: 32070

why don't you use normal way of including javascript in your ERB view.

<%= javascript_include_tag "filename_from_public_javascripts_folder" %>

Upvotes: 0

Wahaj Ali
Wahaj Ali

Reputation: 4113

You need to use the function escape_javascript.

so your code should be:

<%= escape_javascript(render(:partial => 'recurly.js', :locals => { :company => @company }) %>

Upvotes: 1

harald
harald

Reputation: 6126

have you tried:

<%= raw(render :partial => 'recurly.js', ...) %>

Normally ERB will escape the output. Not sure if this is a good solution in the long run, even if it works, though.

Upvotes: 0

Related Questions