Link_to generates a GET in Rails 4 - ruby-on-rails

A rails 4 app. A simple question: why does this genereate a GET request?
<%= link_to("fdsfds", some_delete_path, method: :delete, data: { confirm: "Are you sure fdsfds?" }) %>
routes:
delete '/some-path/some-path123' => 'controller123#method123', as :some_delete
All query libraries are included properly.
html:
<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/fdsfdsfds">dsfdsfds</a>
error:
No route matches [GET] "/fdsfdsfdss"

Your code is good, but your javascript include is probably not. Check that your application.js is including the correct libraries. What you may be missing is 'jquery_ujs' which is what handles confirmation dialogs and non-Get requests.
Here's mine with query.
//= require jquery
//= require jquery_ujs
//= require_tree .
Without the javascript library, your link will be a GET request.

I realize this isn't an answer, but instead a "how do I go about debugging this?"
What does your inspector show? Does it show the request as a DELETE? If no, continue.
Add this to the very bottom of your routes path, update the link_to, and check your inspector if it sends a DELETE request?
controller:
def d
abort
end
routes:
match '/d' => 'posts#d', :via => [:delete]
view:
<%= link_to 'delete', '/d', :method => :delete %>
There may be some conflicting route, which while highly unlikely, there are a lot of moving parts which sometimes cause finicky errors.
If that doesn't answer your question, can you post your routes and view, and perhaps a screenshot of your inspector?

If anyone comes here that uses the bootstrap (v4) gem and always ignored the tether warning in development: You actually need to add the tether gem as advised.
That error stops executing the current js file. If all js files are seperate (as they are in development) this doesn't stop the data-method js part getting executed. In production all js is concat into a single file and thus it won't execute the rest of the file which likely contains the data-method code.

Related

Why does Rails delete method get routed to GET instead of DELETE?

I am getting, the following routing error, i am not sure why it is not routing to DELETE and goes to GET
No route matches [GET] "/signout"
html.erb:
<%= link_to "Sign out", signout_path, method: "delete" %>
routes.rb:
match '/signout', to: 'sessions#destroy', via: :delete
What command am i missing?
You want to ensure you have the jquery_ujs file loaded as part of your application.js manifest. I believe in older versions of Rails it is known as rails_ujs.
application.js
// ...
//= require jquery_ujs
// ...
Check if you load jquery-ujs and allow javascript in your browser.
<%= link_to "link", some_path, method: "delete" %>
will generate
link
As soon as you click the link, jquery-ujs will cancel the action and make a delete request, taken from the data-method attribute:
from link_to:
:method => symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET.

Issues with UJS

I've successfully added ujs in one rails (3.2.6) app. Adding the :remote => true to my form tag allows me to make ajax calls to my js.erb files for dynamic loading of divs, ect..
But in another application on the same machine (Ubuntu 12.0.4), is seems the ujs engine is not working. I'm always getting a Template not Found because the form is sending format => html rather than js. If I force the form to use js format (format => 'js'), it then just renders the js.erb file, rather than calling it via ajax.
In the application.js, I've included the proper headers with the following:
//= require jquery
//= require jquery_ujs
//= require_tree .
The javascript files are included when I actually browse to the primary home page which is using the application layout, which includes the above mentioned javascirpt references. The form in the page is as follows:
<%= form_tag list_path, :remote => true, :id => 'frmBookResults', :method => :post do %>
But although it contains :remote => true, and there's a route established for list_path (the route works, because if I change the list.js.erb to list.html.erb, the view renders), and a method in the controller to handle the request (def list....end), the subsequent list.js.erb is ignored and I get a template not found error, because rails is processing the form request as html, which I can confirm in the log.
I've searched everywhere I could for a solution, but can't figure out why my ujs isn't working for this particular app, when it is nearly identical to my working app, gemset, versions, and configuration.
I've found a couple of other articles on stack overflow where people had the same problem, but no final, working answer was given.
Any help or direction would be greatly appreciated.
It was indeed an issue with the ajax being broken. The onkeyup trigger I was using to submit the form was as follows:
<%= form_tag list_path, :id => 'frmBookResults', :remote => true do %>
<input id='keyword' type='text' onkeyup='document.forms["frmBookResults"].submit();'/>
<% end %>
Note the following:
document.forms["frmBookResults"].submit();
Apparently, submitting the form via javascript was the issue, because when I updated the onkeyup to use a jquery submit as described below, rails ujs kicked in and the ajax calls to my list.js.erb worked:
onkeyup="jQuery('#frmBookResults').submit()"
Thanks for all the feedback mccanff! Your contribution along with other developers from the rails group at linkedIn helped me finally solve my issue.

Following Ruby-on-Rails tutorial and getting 'destroy users' doesn't work

I've recently installed Ruby on Rails 3.2 and have been trying to learn it. I've been following along with the RoR 3.0 tutorial (http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#top) and so far it is going well (yes I know there's a 3.2 version).
Currently I am stuck on section 10.4.2 which teaches how to add a link to destroy users. It says to add the code
<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
:title => "Delete #{user.name}" %>
As well as adding in apps/view/layout/application/html/erb
<%= javascript_include_tag :defaults %>
It seems like this should take it right to the destroy method in the user controller, as the tutorial says but it is not working for me and I cannot figure out why. The link it creates is just to /user/:id. I looked at the same section in the 3.2 tutorial and it is fairly the same directions (but does not have the javascript include tag code). I can't get it to work following that tutorial. So I am not sure why it is not working or how to get it to work.
So we are clear, rather than going to the destroy method in this User controller, it goes to /user/:id which is the show method.
Deleting a resource (a user in your case) requires jquery_ujs javascript file to be included on a page. It is quite common to see a 'show' action being called, because without jquery_ujs is not sending the hidden data that indicates the HTTP DELETE verb.
Try to explicitly insert the jquery_ujs like follows:
<%= javascript_include_tag 'jquery_ujs' %>
and see what happens.
jquery_ujs is designed to be '... unobtrusive scripting support file for the Ruby on Rails framework, but is not strictly tied to any specific backend.'. In other words, it scans the document, sees the special data-* attributes and performs various actions depending on these attributes, for example, appending hidden html elements, performing ajax requests, etc.
Also note, that in order to use jquery_ujs, jquery should be referenced too (before).
Hope this helps.
My problem was that I did not reference jquery. Adding //=jquery fixed it.
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" %>
and your link code should be like this:
<%= link_to "<i class='icon-trash'></i> Delete".html_safe, user, :confirm => "Are you sure you want to delete this user? " + user.name + "?" ,:method => :delete%>
and your controller should have like this:
def destroy
#item = Item.find(params[:id])
if #item.destroy
redirect_to users_path, :notice => "Successfully deleted a user."
else
redirect_to users_path, :notice => "Failed to delete a user."
end
end

default_actions in activeadmin in rails doesnt make "delete" button create destroy link

I have this:
ActiveAdmin.register User do
index do
column :email
column :name
column :role
column "Last Sign In", :last_sign_in_at
column :account
column "Units" do |user|
user.units.count.to_s
end
default_actions
end
The default_actions method should create the show, edit, and delete links. It shows them but the delete link is just a link to the show action:
admin/users/1
Specifications said it should create a delete link.
Dont know why it did that. So I tried an alternative:
column "Delete" do |user|
link_to "Delete", destroy_admin_user_path(user)
end
I get this error:
undefined method `destroy_admin_user_path' for <div class="index_as_table"></div>:ActiveAdmin::Views::IndexAsTable
I even tried adding this in routes:
match "/admin/users/:id/destroy(.:format) " => "admin/users#destroy"
Still got same error.
I included this in application.html.haml:
= javascript_include_tag :all
Still same problems as above.
Thanks for response
This is a bit late but the real real reason your link wasn't working is because you didn't put the :method in your link and instead used "destroy_admin_user_path".
Try this instead:
link_to "Delete", admin_user_path(user), :method => :delete, :data => {:confirm => "Are you sure?"}
This is what works for me, with ActiveAdmin.
I had this problem when I updated the active_admin gem, so I fixed it regenerating the active_admin assets and now the destroy action works fine.
rails generate active_admin:assets
Did you check to see if the full rails.js is added to the javascript? Use firebug to inspect the link and see if it has the data-method attribute. Also inspect the HTTP headers and see if the request is made with DELETE.
If the request is not made using "DELETE" than you have a problem with your javascripts. Check rails.js for integrity and jquery integration. Additionally check your assets.
Could your provide more details about your rails version? Javascripts included in HTML source?
Try another thing, go to assets/javascripts/application.js and add
//= require jquery
to the top if you are running 3.1

ruby on rails - link_to() problem

I made this link in order to destroy a comment :
  <%= link_to 'Destroy Comment', [comment.post, comment],
:confirm => 'Are you sure?', :method => :delete %>
this suppose to send to the destroy action in the comments_controller.
the problem is that it searches for the 'show' action, Instead of the 'destroy' action :
Unknown action
The action 'show' could not be found for CommentsController
Do you think you know why it does that?
Thanks,
Oded
edit: problem solved I used 'button_to'
Rails 3:
When you use JQuery, make sure you have the right rails.js file (https://github.com/rails/jquery-ujs). When you use Prototype, the correct rails.js file is already installed. Also, make sure the following is added in your layout head:
<%= csrf_meta_tag %>
And also make sure that both the JS framework and the rails.js file is being loaded.
<%= javascript_include_tag "jquery", "rails" %>
# or
<%= javascript_include_tag "prototype", "rails" %>
Just a side-note - You can also point to the Googleapis link: http://scriptsrc.net/.
When you use :method => :delete inside a link, the following HTML will be created:
Click me!
As you see, the HTML5 data- attribute is being used. The rails.js file automaitcally puts click events on links with these attributes. When data-method="delete" is set, the request will be done with the DELETE HTTP method. So clicking it will destroy the comment. Also, setting :confirm will create a data-confirm attribute which does what you would expect.
Rails 2:
When you use Prototype, the :method => :delete thing will work automatically. Just make sure you include the right Javascript files:
<%= javascript_include_tag :defaults %>
When using JQuery you should install the 'jrails' plugin (https://github.com/aaronchi/jrails). It allows you to use the same Prototype helpers for JQuery. The plugin uses an old version of JQuery, so make sure you update that one.
I don't know for sure if the :method attribute uses Prototype in Rails 2 or just regular Javascript. So it could be that you don't even need Prototype or JQuery for the :method attribute in Rails 2.
As I said in the comment: I never use button_to for DELETE links. You can just as easily get it working with link_to. And as far as I know it's the helper most people use when creating these kind of links. Hope it helps. :)
ERROR: ActionController::RoutingError (No route matches [GET] "/javascripts/jquery.js")
Solution, download: http://code.jquery.com/jquery-1.6.3.js
ERROR: AbstractController::ActionNotFound (The action 'show' could not be found for CommentsController)
Solution, download: https://github.com/rails/jquery-ujs/raw/master/src/rails.js
In rails 3.1.0 save the above js files to app/public/javascripts/
Rename or remove your existing js files.
I've just solved this problem in my own App (rails 3). I followed the steps for rails 3 and, the most important issue, installed the correct rails.js file in my public/javascripts folder. It didn't work until I've installed rails.js.
The one i chose is this:
https://raw.github.com/rails/jquery-ujs/master/src/rails.js
I just came across this same issue with Rails 3. I'm using jQuery with the updated rails.js file. What fixed it for me was something simple - use :method => :delete, not :method => :destroy.
=link_to( 'delete account', user_admin_path(current_user.id), :confirm => "Deleting your account is irreversible!! Are you sure you wish to continue?", :method => :delete )
And in the header I have:
= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js", "jquery.colorbox-min", "jquery.validate.min", "rails"
Works like a charm :)
Make sure you reference //= require jquery and //= require jquery_ujs (in that order) in your application.js file, in \app\assets\javascripts.

Resources