The script does not connect when I use this code.

show.html.erb

<% content_for :head do %> <%= javascript_include_tag 'comments' %> <% end %> <p> <strong>Title:</strong> <%= @link.title %><br> <strong>Url:</strong> <%= @link.url %><br> <strong>User:</strong> <%= @link.user.email %><br> </p> <%-if user_signed_in?%> <%= render 'comments/new', review: Comment.new(link_id: @link.id) %> <%-end%> <%= render 'comments/show', comments: @comments %> 

In the query console you can see that it should have loaded, but it is not ...

The script name comments.js

enter image description here

Just reloading the page helps or inserting js code in the script tag

Maybe someone has already encountered such a problem. Thank you for the answer)

  • I have already solved this issue, the fact is that it does not want to load due to turbolinks . github.com/turbolinks/… - x_ror

1 answer 1

Your approach is not completely clear or the question is not fully described. Write the code in Ruby and send requests to the database via ajax. Place javascript code in the folder assets / javascript / . rails will load them all automatically.

Why do you need such a code?

 if user_signed_in? = render 'comments/new', review: Comment.new(link_id: @link.id) end 

Why load rails and drive data on the web. Create just a form and if data comes create Comment.new . Try to keep the view clean. Creating class objects is better in the controller. Do not break MVC!

  • So the fact is that there are assets / javascript here, but it doesn’t load from 1 time only if I reload on this view - x_ror