Rails 3 and unobtrusive javascript

I had a problem linking an unobtrusive js file to a view.

I have a name named.html.erb

...
<div id="fooBar">say bye!</div>
...

I also have a js file index.js.erb

$("#fooBar").html("say hello!")

I am currently receiving a bye by message because the javascript file is not executing.

What am I doing wrong?

It seems to me that js.erb is not used for this purpose. And I just need to add the script link to the regular .js file where the specific client logic is stored

Thank you Alex

+3
source share
3 answers

Did you specify render_with: js in the controller?

, , , . html js. Rails UJS, show (), , , JS HTML. .

: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

+2

?

Try

$(function() {
    $("#fooBar").html("say hello!")
});

, jQuery, , , .

+1

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

/layouts/application.html? JS, ( Rails 3).

+1

Source: https://habr.com/ru/post/1760470/


All Articles