ruby on rails - link_to() problem - ruby-on-rails

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.

Related

Rails 6 + Bootstrap - Link_to logout throws an error but button_to works fine. Why is it so?

On adding Bootstrap 5 to my Rails 6 app, logout link which worked fine previously throws an error
<%= link_to "Log Out", destroy_user_session_path, :method => :delete %> AbstractController::ActionNotFound (The action 'show' could not be found for UsersController)
I could fix this by replacing link_to with button_to. But why does link_to throw an error?
You should use link_to only when simply redirecting user to a different page with show or index methods. Here you want to use button_to because it creates a custom form that carries additional data as you're manipulating with the database. You should use it with methods like update delete and create.
It looks like you're missing jquery_ujs in your project. You can install it with a gem or through yarn, depending on what you like to use.
Here you will find some more info on what jquery_ujs is and how it works.

Link_to generates a GET in Rails 4

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.

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

RESTful link_to :method => destroy in Rails 3, with fallback for users with javascript disabled

Is there a simple RESTful way to have a fallback url for the link_to with :method => :delete that will work for folks who have javascript disabled?
For example, suppose I have a list of comments. To add a link to delete a comment, I might put:
<%= link_to "Delete", comment_path(comment.id), :method => :delete %>
This would produce a link with the data-method="delete" that my ujs driver (jquery_ujs in my case) will turn into a DELETE request. However, when javascript is turned off, the link is followed as a GET request (as advertised in the docs), and my controller gets rightfully confused.
Is there a good and simple way to resolve this problem? There's Railscast 77, which shows one solution, but this seems to require an awful lot of extra code and throws out the quite elegant :method => :delete solution. I'd be OK with it producing a link to a confirm-delete page when javascript is disabled.
The only solution I can think of is to use button_to on the page and UJS to replace it with the corresponding link_to. This somehow doesn't seem quite right to me, but maybe it's OK? Any other ideas?
Personally i don't like the idea of mapping the :delete to a :get, it's not RESTFul...The solution i use is the same than the one you have been thinking about, ie a button_to instead of the link_to, and if needed i can use CSS to style the button so that it looks like a link.

rails 3 - link_to to destroy not working

I am trying to create a destroy link to my users controller, I am also using devise.
Here is my code -
View
<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>
Controller
def destroy
if #user = User.find(params[:id])
#user.destroy
respond_to do |format|
format.html { redirect_to account_index_path }
format.xml { head :ok }
end
end
end
Routes
devise_for :users
resources :users, :except => [:new]
The link translates to localhost:3000/users/10
When clicked this opens the users show instead of deleting them
Any ideas ?
Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
use button_to (passing a :method => :delete) instead and style the button appropriately.
Actually I just had the exactly same problem yesterday
Try this:
<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>
It works (for me at least)
In case that you are using jQuery instead of Prototype, you are probably missing a javascript file.
You can find details on how to add it to your project from the jquery-ujs GitHub page or from episode 205 of the Railscasts.
At a guess I think it is because in Rails 3, unobtrusive javascript is now used for functionality such as this (Rails 2 would output a bunch of nasty inline javascript for your code, Rails 3 puts the javascript in an external file, and uses HTML5 data- attributes to interact with that.)
To solve this you need to include <%= csrf_meta_tags %> in your page header to reference the external javascript. It also deals with XSS issues.
Some details here: Delete link sends "Get" instead of "Delete" in Rails 3 view
If you are using jQuery, make sure you have something like this:
<script type="text/javascript">
// this allows jquery to be called along with scriptaculous and YUI without any conflicts
// the only difference is all jquery functions should be called with $j instead of $
// e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
var $jQ = jQuery.noConflict();
</script>
follow the steps in the installation part rails/jquery-ujs
add <%= javascript_include_tag "application" %> in your layout file.
If you haven't included jquery and jquery-ujs in your app , the default link_to default coming with scaffold wont work!
I had the same issue.It got solved after including both these js!
Also if you get this problem in production mode, it may be because you have not compiled the assets. See http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
Worked for me with confirmation message.
<%= button_to 'Destroy', {action: :destroy, id: version.id}, onclick: 'return confirm("Are you sure?")', method: :delete %>

Resources