rails / bootstrap delete link issue: data-method delete not working - ruby-on-rails

I am using gem 'twitter-bootstrap-rails' for rails bootstrap
I have a few links on a page
Rails code
<td><%= link_to 'Destroy', swimming_classschedule, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<li> <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %> </li>
html code:
Logout
<td>Destroy</td>
<td>Destroy</td>
Whenever I click to delete a student I got logged out. I am totally confused

I had the same problem.. I read in the forums that it was due to javascript issues.
In application.js, I previously removed the original files because I thought they weren't needed as I was using bootstrap.js. I put it back in the file and now the delete method works again!
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

based on this answer Delete link sends "Get" instead of "Delete" in Rails 3 view
I added
<%= csrf_meta_tag %>
in header, which fixed the issue

Related

Under Rails 5, using helper link_to with method delete issues a get and object#show instead of object#destroy

I am migrating to Rail 5.2.3 and found that my delete code is failing. When I press the "Delete Object" button, it routes to object#show instead of object#destroy. This code works well under Rails 4, but is failing under Rails 5.
The html.erb module includes:
<%= link_to('Delete Object', {action: :destroy, class: 'btn btn-warning kc-wide'}, method: :delete, data: {confirm: 'Are you sure?', disable_with: "Processing..."}) %>
The generated code is:
<a data-confirm="Are you sure?" data-disable-with="Processing..." rel="nofollow" data-method="delete" href="/admin/objects/414?class=btn+btn-warning+kc-wide">Delete Object</a>
I finally diagnosed the problem as found here (https://github.com/rails/rails/tree/master/actionview/app/assets/javascripts). Since rails-ujs is now contained within Rails 5, I removed references to it from my code. Yes, the old gem should not be bundled within Gemfile. However, to access it, it still must be included within the JavaScript manifest, such as in application.js within the Asset Pipeline.
//= require rails-ujs

If I require jquery over jquery_ujs, my application breaks and vice versa

I need to require both in my application.js file. The problem is if one is above another, parts of my application break. But I need both of them working.
E.G. If I require jquery over jquery_ujs, my log out functionality stops working and I receive a No route matches [DELETE] "/users/sign_out" error.
E.G. Vice Versa, if jquery_ujs is over jquery, some links from devise such as 'destroy' or 'edit' completely disappear.
I believe this to be a problem with devise, but I am unsure what I should do.
Application.js:
//= require jquery_ujs
//= require jquery
//= require bootstrap
//= require turbolinks
//= require masonry/jquery.masonry
//= require_tree .
_header.html.erb:
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "About", about_path %></li>
<% if user_signed_in? %>
<li><%= link_to "New Pin", new_pin_path %></li>
<li><%= link_to "Account Settings", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "Sign in", new_user_session_path %></li>
<% end %>
</ul>
Update: Obviously, when I logout, it doesn't work and gives the error No route matches [DELETE] "/users/sign_out. However, if I refresh the page I get a successful logout :/ everytime.
it seems like you are adding jquery_ujs before jquery. so you need to add jquery first something like
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require masonry/jquery.masonry
//= require_tree .
Try this, paste this code into devise.rb
config.sign_out_via = :get

the link_to with confirm works only after reloading the page

**<%= link_to "Delete",(#article) ,method:"delete",data: { confirm: 'Are you sure you want to delete ?' } %>**
I have a rails app which lets the user create an account and posts delete and update add comments and get notifications using rails and Jquery
when I click the buttons which include confirm messages do not work until I refresh the page !!!why ?? what i should do ??
<%= link_to "Delete",(#article) ,method:"delete",data: { confirm: 'Are you sure you want to delete ?' } %>
My first guess is that your jquery is interfering with the link. Make sure you do not have a "prevent_default" clause in your js file targeting that link or that would prevent the normal javascript from executing when the link is clicked. Make sure your application.js file still has:
//= require jquery
//= require jquery_ujs
Lastly, make sure in your layouts/application.html.erb file you still have:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Once that is done, reload the page and check your developer console to make sure that there is a data-confirm attribute in the link tag such as:
<a href="/articles/1" data-confirm="Are you sure?" data-method="delete">

Rails HTML requests :Post but does a :Get

I have some code for attending/withdrawing from a competition:
<% if #competition.users.exclude?(#user) %>
<%= link_to 'Attend Competition', attend_competition_path(#competition.id), :method => :post %>
<% else %>
<%= link_to 'Withdraw', withdraw_competition_path(#competition.id), :method => :post %>
<% end %>
When I click on the action I go to an error page:
No route matches [GET] "/competitions/1/withdraw"
Why isn't it doing a POST request? How do I fix this?
Not sure if it effects it, but my current js is
//= require jquery
//= require bootstrap
//= require turbolinks
//= require_tree .
thanks

Destroy link in Rails 3.2 shows the data instead of deleting it

I created a new scaffold rails generate scaffold scores score:integer route_id:integer and ran rake db:migrate.
This works well, but when I go to the scores page to delete some data the delete link somehow displays the view details, but doesn't delete it. I also compared the link with some other projects and it looks pretty much the same only that it doesn't work this time.
That's what I have in my index.html.erb page:
<td><%= link_to 'Destroy', score, method: :delete, data: { confirm: 'Are you sure?' } %></td>
I found in some posts on this site that the gem file needs to contain gem 'jquery-rails' so I checked and it is there. I also ran bundle install again, but no change.
The change from link_to to button_to didn't help either.
Does anybody have an idea what else it could be? If you need more details let me know and I will be happy to add it.
Many thanks.
Sometimes it happens because you haven't included jquery_ujs in your application. Ope the browser console and check if jquery_ujs is included or if it is included multiple times.
I had the same problem and I have managed to solve it only after I have added both jquery and jquery_ujs to my page:
<%= javascript_include_tag "jquery" %>
<%= javascript_include_tag "jquery_ujs" %>
<%= javascript_include_tag "application" %>
I most probably have a problem with the asset pipelines as I have requested both of them in the application.js but it didn't work when they were not explicitly requested on the page.
In my application.js I have:
//= require_jquery
//= require_jquery_ujs
//= require_tree .
I'm guessing that the method: :delete is being overridden or ignored since you're providing the resource. Could you try this?
<td><%= link_to 'Destroy', destroy_score_path(score), data: { confirm: 'Are you sure?' } %></td>
Here is the documentation for link_to for reference:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Simple.
You need to include jquery_ujs also named as rails.js in your application.html.erb.
I have this issue before. Rails needs rails.js to do this.

Resources