the link_to with confirm works only after reloading the page - ruby-on-rails

**<%= 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">

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

Rails 4 link_to Destroy not working in Getting Started tutorial

I am working through the Getting Started tutorial (creating a Blog) and the link_to Destroy is not functioning properly. In the terminal it always interprets it as #SHOW.
In reading similar issues I have learned that the Delete must be converted to a POST for the browser to interpret it. This doesn't seem to be happening.
I have run into the same problem using Destroy in the Lynda.com Rails course as well, so it leads me to believe it is something in my development environment. I am using Rails 4, Ruby 2.00p274, MySQL, WEBrick for the HTTP server on a MacBook Pro Lion.
in the terminal session when Destroy is selected:
Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
Processing by PostsController#show as HTML
Parameters: {"id"=>"4"}
Post Load (0.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "4"]]
Rendered posts/show.html.erb within layouts/application (0.4ms)
Completed 200 OK in 13ms (Views: 8.6ms | ActiveRecord: 0.6ms)
In the ports-controller.rb:
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to action: :index
end
In the index.html.erb:
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
In the routes.rb
Blog::Application.routes.draw do
resources :posts do
resources :comments
end
root to: 'welcome#index'
end
Try this
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
Make sure gem 'jquery-rails' is in gemfile and jquery_ujs is included in app/assets/javascripts/application.js file
//= require jquery
//= require jquery_ujs
Source: https://github.com/rails/jquery-ujs
I had the same issue, for rails 4.2.X. Checked all my javascript files but could not make it work. if u look closely at the server request u will be missing 'authenticity_token' in the params, so user gets logged out.
In rails 4.1 and above u have to use button_to instead of link_to
I solved the problem, just by adding in assets/javascripts/application.js
//= require jquery_ujs
Make sure that you have a Delete REST method via rake routes.
DELETE /articles/:id(.:format) articles#destroy
I've got the same problem only with this blog version as I'm doing both.
http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/
I'm also trying to learn how to use the web console and pry within it.
My problem is that binding.pry doesn't show up in destroy method but will in show. That tells me it must be a bad link right? It's not getting to the destroy method even. Thank you all for your answers. We do have to try things don't we?
Trying
<td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
is not going to work for that one.
Here's what he has
<td><%= link_to 'Destroy', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete %></td>
because you only want to delete the comment here.
But combining things so far like the following gives no errors but I still have no binding.pry from the destroy method in the controller.
<td><%= link_to 'Destroy', [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' } %></td>
and I never get the confirmation like you would expect.
So do try to figure out what's going on with the jquery as suspect.
I can confirm that was my case but the rest I'll leave for nubes because this will show up in a google search.
Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
This is the problem. The delete link is using GET http verb even though you used method: delete in your link.
Check out the following SO thread
include below line in js
//= require jquery_ujs
include gem:
gem 'jquery-rails'
Destroy Link:
<%= link_to "Logout", destroy_user_session_path %>
I had the same issue and realized that I had created my rails app with -J which is the same as --skip-javascript.
You need JavaScript enabled for this to work; the lack of a popup to confirm the delete is a dead giveaway.
I had exactly this problem and it was caused by triple-clicking the Guide's code block and copy/pasting the code straight into my editor (ST2 on OSX).
You should check that the generated HTML for the link looks like this:
<a data-confirm="Are you sure?" data-method="delete" href="/posts/3/comments/3" rel="nofollow">Destroy Comment</a>
If it doesn't, but instead looks like the below, then the Javascript won't be applied:
Destroy Comment
The large gaps between the href and the unparsed Ruby are caused by the non-breaking spaces (unicode character \u00a0) used in the Guide being copied into your script.
I'm not sure why this doesn't cause a script parse error.
I had the same problem as Barry. Make sure you're copy/pasting correctly in your /app/views/index.html.erb file and inspect the html that is rendered.
I had same problem. In my case, a i had change \app\views\layouts\application.html.erb file from
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
to
<%= stylesheet_link_tag 'default', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
to avoid my very first Rails problem. But it apparently mangled JavaScript execution and caused Destroy issue.
Therefore rollback your \app\views\layouts\application.html.erb file to its original state, and treat this problem as here
Rails ExecJS::ProgramError in Pages#home?
in evedovelli answer (about coffeescript gem in windows).
I got the same problem. I work out by this way:
My env. is:
A. ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32]
B. Rails 4.2.5
C. Browser: Firfox 44.02
add include js in html page because delete function will use js to handle.
<%= javascript_include_tag "application" %>
add skip_before_action :verify_authenticity_token to controller for prevent InvalidAuthenticityToken in AdsController#destroy
add link_to into your web page
<%= link_to 'Delete', post, method: :delete, data: {confirm: "Are you sure?"}%>
Now, I can use link_to to delete a record from my page.
Using the button_to method instead of link_to seemed to work, minus the fact that it was not confirming(which I felt was important).
In my case, I was using react-on-rails and had multiple layouts.
Adding <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> to my hello-world layout fixed the problem.

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

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

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.

Rails PJAX not working

I have added the pjax javascript.
In my view I have:
<div data-pjax-container>
<%= yield %>
</div>
But still onclick the hole page updates.
Be sure you have included this line in app/assets/javascript/applications.js:
//= require jquery.pjax
Add an id attribute to the div:
<div id="target_pjax_container" data-pjax-container>
<%= yield %>
</div>
And make an explicit call to a pjax div from a link in your views:
<%= link_to 'user', user_path, :'data-pjax' => '#target_pjax_container' %>
Note:
The gem used is 'pjax_rails'. Always check that javascript isn't broken (with firebug or equivalent - You can also use it to sniff the network and view the pjax request).
Maybe you forget to add the line:
//= require pjax
to app/assets/application.js?

Resources