ruby on rails - Delete database row in ROR -


can me why following 2 pieces of code not yield same 'result'?

option 1

<%= button_to 'delete', {:controller => "articles", :action => 'destroy', :id => article.id},    :confirm => "are sure want delete?", :method => :delete %> 

option 2

<%= link_to 'delete', {:controller => "articles", :action => 'destroy', :id => article.id},    :confirm => "are sure want delete?", :method => :delete %> 

option 1 works. option 2 reason calls show action in controller. funny thing option 2 used in ruby on rails tutorial, following....

as can imagine novice ror.

in order link_to work delete method, rails needs unobtrusive scripting adapter jquery.

  • make sure gemfile has

    gem 'jquery-rails'

  • make sure app/assets/javascripts/application.js has

    //= require jquery
    //= require jquery_ujs

  • make sure app/views/layouts/application.html.erb has

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

    inside head tag. remove 'data-turbolinks-track' => true section if don't plan use turbolinks.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -