Oded Harth
Oded Harth

Reputation: 4406

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'

Upvotes: 2

Views: 2671

Answers (5)

RailsGuy
RailsGuy

Reputation: 11

Make sure you reference //= require jquery and //= require jquery_ujs (in that order) in your application.js file, in \app\assets\javascripts.

Upvotes: 0

varker
varker

Reputation: 111

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 :)

Upvotes: 0

myk
myk

Reputation: 11

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.

Upvotes: 1

viktorvogh
viktorvogh

Reputation: 51

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

Upvotes: 0

RobinBrouwer
RobinBrouwer

Reputation: 973

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:

<a href="/comments/1" data-method="delete">Click me!</a>

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. :)

Upvotes: 5

Related Questions