Rails 4, link_to does not work with destroy action when jquery, jquery_ujs is required

Why is the destroy action incompatible as soon as jquery is required in the application.js file? How can you reactivate the destroy action again without regard to jquery?

index message view:

h1 Blog - @posts.each do |post| h2 = link_to post.title, post p = post.content p = link_to 'Edit', edit_post_path(post) p = link_to 'Delete', post, data: {confirm: "Are you sure?"}, method: :delete br p = link_to 'Add a new post', new_post_path 

kill action in message controller:

 def destroy @post = Post.find params[:id] @post.destroy redirect_to posts_path, :notice => "Your post has been deleted" end 

application.js:

 = require jquery = require jquery_ujs //= require turbolinks //= require_tree 

As soon as I comment on these two, the destruction action works again. Without comment, the delete link just launches the show action ... your thoughts?

+6
source share
3 answers

So, the asset pipeline is doing something special with these comments. Having a form comment

 //= require jquery 

instructs the preprocessor to include this file when creating the .js application, as served by the application clients. When you delete a comment, you delete this behavior - and the resulting libraries are not compiled into the final application.js application served by the application.

As a result of this, you misinterpret how the required directive works. It ONLY works inside the comment. When you remove the comment tag, you stop the inclusion of the library.

+3
source

application.js is a manifest file, so the syntax should have these comments. See http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

0
source

If you copied and pasted the code from http://guides.rubyonrails.org/getting_started.html , try entering it instead.

Or remove all leading "spaces" that cannot be spaces. Somehow I got ascii 160 instead of ascii 32 (space character).

I'm sure this will happen, but copy and paste it again, now it works great.

0
source

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


All Articles