Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4, link_to not working with destroy action when jquery, jquery_ujs is required

How come the destroy action is incompatible once jquery is required in the application.js file? How do you get the destroy action to work again without disregarding jquery?

posts index 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

destroy action in posts 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 those two out the destroy action works again. Without the comment, the delete link just fires the show action...your thoughts?

like image 431
Ryan Waits Avatar asked Oct 16 '13 02:10

Ryan Waits


1 Answers

So the asset pipeline does something special with those comments. The presence of a comment of the form

//= require jquery

instructs the preprocessor to include that file when building the application.js as served up to clients of the application. When you remove the comment, you're removing this behavior - and the resulting libraries are not compiled into the final application.js served up by the app.

The upshot of this is that you're misinterpreting how the required directive works. It ONLY works inside a comment. When you remove the comment tag you stop including the library.

like image 61
Peter Goldstein Avatar answered Oct 20 '22 00:10

Peter Goldstein