Delete link sends "Get" instead of "Delete" in Rails 3 view - ruby-on-rails

I'm using Rails 3 and have have a page that outputs a list of posts from the database. I'd like to be able to delete a post from a link.
The 2nd example below works, but the first doesn't. Anybody know why the first won't work? My view contains:
# this link sends a "GET" request which asks for the #show function
<%= link_to 'Delete', post, :method => :delete %>
# this link sends the proper "DELETE" request which asks for the #destroy function
<%= button_to 'Delete', post, :method => :delete %>
My routes file contains the following:
resources :posts

Make sure that your <head> section in the layout includes the following:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
In Rails 3, the delete requests are being handled with the help of JavaScript to ensure that the request is being sent correctly. If you don't have the csrf meta tags and the required JavaScript code, delete links won't work. The :defaults JavaScript files include--among others--prototype.js, and application.js. The latter contains the stuff that makes the link work, but relies on the Prototype framework.
If you don't want to use the default Prototype JavaScript libraries, there are ports of application.js for several other frameworks. You can find the jQuery one here, for instance.
Buttons will still work regardless of JavaScript, as the HTML code generated with button_to includes all necessary information by itself. This is why you're seeing the button work while the link doesn't.

looks like
<%= javascript_include_tag :defaults %>
is no longer supported
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/JavascriptTagHelpers/javascript_include_tag
Instead, you should use
<%= javascript_include_tag 'application' %>

I had the same problem in rails 5.2.3 and this helped me:
layout head:
<%= javascript_include_tag "rails-ujs" %>
config/initializers/assets
Rails.application.config.assets.precompile += %w( rails-ujs.js )

In my case, I was using following.
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
I was also using jQuery by including jQuery just below default prototype JS. That was causing some sort of conflict.

Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
You can style the button appropriately for delete method or Use Form.

I had the same issue in Rails 6.
The solution by Romalex to include:
<%= javascript_include_tag "rails-ujs" %> in head tag &
Rails.application.config.assets.precompile += %w( rails-ujs.js ) in config/initializers/assets also worked to solve an issue with destroy_user_session in devise which would not DELETE.
So this solution also applies to this question Ruby on Rails - devise users/sign_out not working

This will work. The issue is the fact that you need to pass it the right path so the route gets it.
<%= link_to 'Delete', post_path(post.id), :method => :delete %>

In my case with Rails 5, I was using following in my layout:
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
In this way:
... </title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
...
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

Try this.. its a simple change..
add this to your route.rb file
get '/lists/:list_id/comments/:id' => 'comments#destroy'
I made this change and now its working..

In my case.....
In head:
<%= csrf_meta_tag %>
In config/initializers/assets.rb:
<%= javascript_include_tag :defaults %>
Was the answer! Great call!

Related

Why is this COMMENTED piece of code, <%= stylesheet_link_tag "application", :media => "all" %>, not allowing my ROR app to run

I'm in the process of getting Twitter-rails-bootstrap GEM up and running on my Windows OS.
The load error is below:
cannot load such file -- less
(in C:/Sites/cardMS/app/assets/stylesheets/bootstrap_and_overrides.css.less)
Thanks
To comment some rails code you need append hash although it is in html comment block.
So change
<%= stylesheet_link_tag “application”, :media => “all” %>
to
<%#= stylesheet_link_tag “application”, :media => “all” %>
I just tested Nitin's answer in a sample app and looked it up. Indeed all you need to do to comment any erb is place a hash sign after the opening '<%' tag. I suppose the erb parser doesn't care about normal ruby comments as those too can have dynamic parts. This indeed happens when you generate a scaffold and the commented route "#get /posts/1" shows the correct resource. Rails generators do support this feature with templates.
<%= not commented blah %>
becomes
<%#= commented blah %>

Rails 3.2.13 link_to post request doesn't work links to the index page instead

I am having problems with the like_to method to make a post request.
I am following the book Agile web development with rails 4th and try to make a post request if the user click on the image.
My code is as following
<%= link_to(image_tag(product.image_url), line_items_path(:product_id => product), :method => :post) %>
Some people said that we need to do some configuration of JS tag to
<%= javascript_include_tag :defaults %>
I did it, but the link is still go to the index page.
I also change the symbol to "create" or "post", but still doesn't work.
Does anyone know how to solve this issue? I have been stuck on this for long and can't find a way out. Thank you very much in advance.
It's confusing, you should take things one by one.
First, I don't understand why you add this tag <%= javascript_include_tag :defaults %>. Normally you should have in you layout/application.html.rb file a line that look like this <%= javascript_include_tag :application %>. This line is the inclusion of your JS manifest and in a default configuration it should be enough. You can read more about assets pipeline and JS manifests in Rails guides (see links bellow)
Second, about your link, I suggest this syntax instead:
<%= link_to(line_items_path(product), :method => :post) do %>
<%= image_tag(product.image_url) %>
<% end %>
As you can see I moved image_tag in a block, its much easier to read. Also line_items_path should work by passing directly the product record to it (no need for a hash, Rails will do the magic).
Please try, and is this does not work out of the box, edit your question to include your route.eb file
Guides Rails 4: http://guides.rubyonrails.org/
Guides Rails 3: http://guides.rubyonrails.org/v3.2.13/

Action Destroy redirect to Show Action in Rails

I am building a link to delete:
I tried 3 different forms but all of them redirect to Show action:
<%= link_to 'delete',{ :controller => 'admin/brands',:action => 'destroy',:id => brand.id } %>
<%= link_to 'delete',brand,:method => :delete %>
<%= link_to 'delete',:id => brand.id,:method => :delete %>
The problem is when I click the delete link it redirects to Show action. I have searched and found something like:
//= require jquery_ujs add in aplication.js - Correct !
metatags added in layout:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Correct!
I also found this option:
config.serve_static_assets = false
I'm using Development enviroment(I think) and not found this option; therefore I don't know why when I click the delete link app redirects to action Show.
UPDATE:
My routes are:
DELETE /en/admin/pages/:id(.:format) admin/pages#destroy {:locale=>"en"}
DELETE /admin/pages/:id(.:format) admin/pages#destroy {:locale=>"pt-BR"}
When I click on any Delete link it redirects me to action Show
Solve this executing rake assets:clean, thanks all.

No route matches assets

Whenever I try to upload a file with Carrierwave + s3 on my Heroku app I get this error "We're sorry, but something went wrong.", looking into my log I see this error:
2012-08-20T21:18:56+00:00 app[web.1]: Started GET "/assets/" for 24.90.124.181 at 2012-08-20 21:18:56 +00002012-08-20T21:18:56+00:00 app[web.1]:2012-08-20T21:18:56+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/assets"):
I've did everything in this tutorial to deploy my app to Heroku on the Cedar stack, but I still see that error. I also had to add this to my application.html
<%= javascript_include_tag :defaults %> <%= javascript_include_tag "jquery" %> <%= javascript_include_tag "nested_form"%> <%= javascript_include_tag "application"%>
because having <%= javascript_include_tag "application" %> only loaded "application.js"
I've tried everything I come across trying to get my uploader to work, but I don't know what the problem is. Everything works perfectly on my local app.
This is my app on github
My Heroku app
you can easyly replace the
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag "jquery" %>
<%= javascript_include_tag "nested_form"%>
<%= javascript_include_tag "application"%>
with: <%= javascript_include_tag "application" %> if your application.js file do this imports.
the default imports are:
//= require jquery
//= require jquery_ujs
//= require_tree .
if the problem is a missing javascript this should fix it
Other problem i see was this link inside app/views/deals/show.html.erb:
<%= image_tag(#deal.user.logo, :class => 'company_logo') %>
this line is returning this html:
<img alt="Assets" class="company_logo" src="/assets/">
this img src is invalid, and that is the error your are receiving on log.
if this image tag could link to an image path, like /assets/avatar.png i believe your problem will be fixed
Since you are using Carrierwave, you need to do the following:
<%= image_tag(#deal.user.logo.url, :class => 'company_logo') %>
or
<%= image_tag(#deal.user.logo.current_path, :class => 'company_logo') %>

Rails 3 link_to (:method => :delete) not working

I'm having trouble with my verbs in Rails...
viewing a page for a resource (Dog) which has_many (Fleas). Embedded in dog's show.html.haml is a call to render #dog.fleas which automatically(?) finds & uses the template in "fleas/_flea.html.haml" to list each flea associated with said dog.
this displays correctly. whew! Now, next to each flea I've put a "Kill Flea" link that goes to a url: //localhost:3000/dogs/1/fleas/7. Which is generated by:
= link_to("Kill Flea", [ flea.dog, flea ], :method => :delete, :confirm => "Sure? A bunny will die")
but every time that link is clicked there is no confirmation... and it renders the flea's show.html page. it's as if it's using GET on /dogs/1/fleas/7 instead of DELETE?!?
ps- not worried about spiders & robots deleting things in my database... i'm just trying to learn Rails..and understand what's happening
Rails 3 uses unobtrusive javascript now. In Rails 2.3, erb would just shove all that messy javascript right into the link itself, in an onClick event. Now the javascript has been moved out of the link, and into external js files. Make sure you have this in your layout:
<%= javascript_include_tag :all %>
If you do have this, there might be deeper problems keeping your javascript from running, but this is the place to start. Let me know how it turns out.
Hi you can also try this:
application.html.erb:
<%= javascript_include_tag 'jquery_ujs' %>
OR
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>
Check your <%= javascript_include_tag %> in application.html.erb
Perhaps you are missing the "application" js
It must look at least like
<%= javascript_include_tag "application" %>
Here I am not using the javascript_include_tag :all, :application etc.
I am using my own js files, with another custom name: javascript_include_tag :my_custom_file and I also had this issue. And I am not using '= require jquery' in the file, but javascript/jquery is working. I think Rails includes jquery by default. What I done is I changed the
link_to "Logout", destroy_user_session_path, :method => :delete
to
form_tag destroy_user_session_path, :method => :delete, :id => "logout_form"
link_to 'Logout', "#delete", :onclick => "$('#logout_form').submit();"
Now its working fine.
check if application.html.erb
<%= javascript_include_tag :application %>
not as:
<%= javascript_include_tag :default %>
Looks like your link_to method isn't quite right. Try using this code instead:
<%= link_to 'Kill Flea', [flea.dog, flea], :confirm => 'Sure?', :method => :delete %>

Resources