Why Destroy Action is NOT being fired? - ruby-on-rails

I am using Rails 3 and trying to see why the Destroy action is not being fired! My html fired is defined as follows:
<%= link_to "Delete", :action => "destroy", :id => article, :method => :delete, :confirm => "are u sure?" %>
And here is the ArticlesController:
def destroy
#article = Article.find(params[:id])
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url }
end
end
When I click on the "Delete" link it takes me to the show action. I am not sure why is that?

Your link_to should be this:
<%= link_to "Delete", #article, :method => :delete, :confirm => "are u sure?" %>
This will generate the correct URL for your article and go to the destroy action.

Have you deleted the javascripts directory which resides in the public directory of your rails app?
If yes, create a new rails project and copy the javascripts directory in your actual project

Make sure your application.js has included this line:
//= require jquery_ujs
That solved my issue.

That first line should be
#article = Article.find(params[:id])
Once you make this change, you won't be able to redirect to #article, since it will be gone; you'll probably need to replace that last line with redirect_to articles_url. The whole new method will be:
def destroy
#article = Article.find(params[:id])
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url}
end
end
If you have trouble with stuff like this, try creating a scaffolded model with rails generate scaffold Test to see what the default options are.

It could also be this Why are default javascript files required to create a destroy link in rails?.

Related

Delete method returning error after devise logout

I'm running into a problem that I'm not exactly sure how to fix.
I have a simple to do list application with AJAX functionality on methods such as 'new', 'create', 'complete', 'delete', as well as Devise authentication.
When I first enter a new session with a User, all of these methods work without a problem. Additionally, the tasks are saved to only the user account, which is perfect.
However, when I log out of an account, and then log back in, the delete method no longer works. I receive the following error:
ActiveRecord::RecordNotFound (Couldn't find Task with 'id'=)
My tasks_controller.rb is below:
class TasksController < ApplicationController
def index
#task = current_user.tasks.all
end
def new
#task = Task.new
respond_to do |format|
format.js
format.html
end
end
def create
#task = current_user.tasks.new(task_params)
#task.save
respond_to do |format|
format.html
format.js
end
end
def update
#task = current_user.tasks.find(params[:id])
#task.toggle :complete
#task.save
respond_to do |format|
format.html
format.js
end
end
def destroy
#task = Task.find(params[:id])
#task.destroy
respond_to do |format|
format.js
format.html
end
end
private
def task_params
params.require(:task).permit(:id, :title, :complete)
end
end
I'm not exactly sure how to fix this problem. Would anyone have an idea on what is going wrong here?
EDIT:
I noticed that on my index page, I have a link to destroy the user's session at the top:
<%= link_to "Log Out", destroy_user_session_path, :method => :delete %>
I'm wondering if rails is having some trouble with this as both the logout link and the delete link are referencing the same method. If so, how can I change the name of the delete method for Task?
<div class="delete"><%= link_to "X", task_path(#task), method: :delete, remote: true %></div>
What is #task referencing? It looks to me like you've set #task to a collection #task = current_user.tasks.all.
Which would be why your delete method can't find a specific record to delete.
-EDIT-
Change #task in your index controller to #tasks as it is a collection.
In your view, do something like:
<% #tasks.each do |task| %>
<div><%= task.title %><div class="delete"><%= link_to "X", task_path(task), method: :delete, remote: true %></div></div>
<% end %>
The key here is that you have task_path(task) which is referencing a specific task id as opposed to task_path(#task) which is referencing a collection of tasks.

Delete action without scaffolding

Status resource (generated by "rails g scaffold Status")
resources :statuses
Link destroy object
<%= link_to "delete", status, :confirm => "are you sure?", :method => :delete%>
layout.html.erb
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Application.js
//= require jquery
//= require jquery_ujs
Rails 4.0.0
When I click on this it redirect me to Rails'scaffold page (Statuses' listing). How I could redirect user to homepage, or better, on previously page?
in statuses_controller.rb you should have destroy action
def destroy
#status = Status.find(params[:id]) # or find(params[:status_id]) depends on how route is built
#status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
change the redirect_to statuses_url to redirect_to :back it will get you back from where your request started or redirect_to root_url for home page.

Updating a record with Rails remote function

I just wanted to know if there is any method available to update a record of a table from a view? For example am I able to delete a record from the table using the code below?
<td><%= link_to 'Delete', my_path(user), :confirm => 'Are you sure?', :method =>:delete, :remote=>true %></td>
And in my controller I have:
def destroy
#user = User.find(params[:id])
#user.destroy
respond_to do |format|
format.js do
render(:update) { |page| page.reload }
end
end
end
The above code works perfectly. It deletes the record from the table and also reloads the page after clicking the destroy link. Now, my question is: can I do the same for updating a value in my record? If so, how can I do that?
Sure you can, you would simply change a few things:
The method in your controller would be update:
def update
#user = User.find(params[:id])
#user.update_attributes(params[:user])
respond_to do |format|
format.js do
render(:update) { |page| page.reload }
end
emd
end
Then you would change your link to something like the following:
<td><%= link_to "Update", my_path(user), :method => :put, :remote => true %></td>
Mind you, this is under the presumption that you are in a form_for #user tag.
<%= form_for #user do |f| %>
<%= f.text_field :name %>
# aforementioned link to update goes here
<% end %>
This is under the impression that you setup #user in the controller method (most likely def edit)
def edit
#user = User.find(params[:id])
end

Ruby on Rails link_to :method => :delete syntax

This is a nub question. I have a resource Project which has_many Feeds. When I am viewing the list of nested Feeds on the Project page, I have a Delete button to remove that Feed from the Project. It works with this syntax:
<%= link_to 'Delete', [feed.project, feed], :confirm => 'Are you sure?', :method => :delete %>
But that then directs to the page listing all the Feeds, and I instead want it to redirect back to the Project page the user was just at. Is it a matter of changing [feed.project, feed] or is it something else? I don't quite understand the syntax of link_to well enough yet.
EDIT:
In my feeds_controller.rb, I changed the redirect line to :back
def destroy
project = Project.find(params[:project_id])
#feed = project.feeds.find(params[:id])
#feed.destroy
redirect_to :back
respond_to do |format|
format.html { redirect_to :back }
format.xml { head :ok }
end
end
You must have a look at the controller for this resource. It should be somewhere like app/controllers/projects_controller, where's there should be an action named destroy. The code that do the redirect must be in there. You'll have to change the following line:
redirect_to project_feeds_url(project)
to this
redirect_to :back
in your controller
def destroy
#project = Project.find(params[:project_id])
#feed = #project.feeds.find(params[:id])
#feed.destroy
redirect_to #project
end

No route matches {:action =>"destroy"

I'm getting the following error when trying to destroy a user's vote on a "contribution":
No route matches {:action=>"destroy", :controller=>"contribution_votes",
:id=>#<ContributionVote contribution_id: 8245, user_id: 40>}
But have no idea why. Here is the button sending the request
<%= button_to "undo voteup!",
contribution.contribution_votes.find_by_user_id(current_user),
:method => :delete, :class => 'contribution_vote undo' %>
Here's the "destroy" action in the controller:
def destroy
#vote = current_user.contribution_votes.find(params[:id])
#vote.destroy
#contribution = Contribution.find_by_id(#vote.contribution_id)
#contribution_decrement = #contribution.decrement(:votes)
if #vote.destroy and #contribution_decrement.save
respond_to do |format|
format.html {redirect_to :back}
format.js
end
else
redirect_to :back
end
end
(some redundancy here I know, but it's to be filled in at a later date)
And here's the setup in routes.rb
resources :contribution_votes, :only => [:create, :destroy]
Can anyone help? I suspect the answer's obvious but I can't find it anywhere on SO.
Change your code in view to (I think it will help):
<%= button_to "undo voteup!",
contribution_votes_path(current_user.id),
:method => :delete, :class => 'contribution_vote undo' %>
.. by the way: type rake routes in your console and verify that you use right route path (I can mistake).

Resources