Ruby on Rails - Destroy Action returns 404 - ruby-on-rails

I have some routes setup like this in route.rb
resources :users do
resources :lists do
resources :tasks
end
end
When I run 'rake routes' I can see the route for my destroy action:
DELETE /users/:user_id/lists/:id(.:format)
My implementation of this action looks like this:
def destroy
#user = User.find(params[:user_id])
#user.lists.find(params[:id]).destroy
render json: { status: "success" }
end
If I comment all the lines of this action except the 'render' line I will get the JSON result back. However with the lines in I recieve a 404. I'm pretty new to rails. Can someone please tell me what I have wrong?
Thanks

Can you post what you have in your view? I suspect you need to specify a method in your link, e.g.: <%= link_to "Delete", [#user, list], method: :delete, remote: true %>

try to use this may be it's helpful for you
<%= link_to 'Destroy', [user,list], method: :delete, data: { confirm: 'Are you sure?' } %>
in your controller
def destroy
#user = User.find(params[:user_id])
#list = #user.lists.find(params[:id]) if #user.present?
if #list.present? && #user.present?
#list.destroy
status = "success"
else
status = "fail"
end
render json: {status: status}
end

Try this
List.delete_all(["ID = ? AND user_id = ?", params[:id], params[:user_id]])
or destroy_all

Related

Can't get destroy to work in rails

Fairly new to rails and can't seem to get this simple destroy action working. All it does is redirect to the mod panel index page and doesn't destroy the record. Do I need to call .destroy in the destroy method? or is there something I'm missing?
mod_approval controller
def index
#guide = Guide.friendly.find(params[:guide_id])
#check_category = CheckCategory.where(guide_id: #guide.id).all
#category = Guide.friendly.find(#guide.id).categories.new
end
def destroy
redirect_to guide_mod_panel_mod_approval_index_path(#guide)
end
config/routes.rb
match '/guides/:guide_id/mod-panel/approve/reject' => 'mod_approval#destroy', :via => :delete, as: :guide_mod_panel_approve_destroy
index.html.erb
<% #check_category.each do |category| %>
<%= link_to "Reject", guide_mod_panel_approve_destroy_path(#guide, category), method: :delete, data: {confirm: "You sure?"} %><br>
<% end %>
You need to fetch the record from the database, then you can call destroy on the object, you can do this
def destroy
guide = Guide.find(params[:guide_id])
category = guide.categories.find(params[:id])
category.destroy
redirect_to guide_mod_panel_mod_approval_index_path(guide)
end
Hope that helps!
You should have to destroy object before:
def destroy
#destroyed_object.destroy # or Model.destroy(params[:id])
redirect_to guide_mod_panel_mod_approval_index_path(#guide)
end

Destroy action error: param not found: link

I am building a mock up of reddit. When on my show view that displays the link & title I submit I have included an option(link) to destroy the submission.
When I click on the destroy I get:
ActionController::ParameterMissing in LinksController#destroy
param not found: link
on line:
params.require(:link).permit(:url, :title)
This is my full Links controller:
class LinksController < ApplicationController
def index
#link = Link.all
end
def show
#link = Link.find(params[:id])
end
def new
#link = Link.new
end
def create
#link = Link.new(link_params)
if #link.save
redirect_to #link
else
render action: 'new'
end
end
def destroy
#link = Link.find()
if #link.destroy
redirect_to index: 'action'
else
render show: 'action'
end
end
private
def link_params
params.require(:link).permit(:url, :title)
end
end
This is my show view:
<h1> This is the show view </h1>
<%= #link.title %>
<%= #link.url %>
<%= link_to 'Edit', edit_link_path(#link) %>\
<%= link_to 'Destroy', (#link), method: :delete, data: { confirm: 'Are you sure?' } %>
I'm really confused, have tried multiple things but can't get it to work. How can i fix this?
Looks like you permit is missing :id and your destroy is missing params[:id] in the find clause.
Your problem is:
def destroy
#link = Link.find()
...
You need something like:
def destroy
#link = Link.find(params[:id])
What's happening is that your Destroy link is sending a DELETE request to some URL like /link/23 with 23 being params[:id]. You need to find the appropriate Link object by using Link.find(params[id]) before you destroy it (similar to how you would in show).

RoR Empty Cart/Delete line_Items

I'm still pretty new to RoR and have run into a problem I can't seem to solve. I've already built the functions to add new products to the database and allow a user to add individual items to their cart, my delete method just gives me an error saying it can't find the delete method in the carts_controller. Any help would be appreciated.
\app\views\cart\show.html.erb
<%= button_to 'Empty Cart', #cart, method: :delete,
data: { confirm: 'are you sure?'} %>
\app\controllers\carts_controller.rb
def destroy
#cart.destroy if #cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is empty' }
format.json { head :no_content }
end
end
private
def set_cart
#cart = Cart.find(params[:id])
end
Error Message Given
Unknown Action
The action 'destroy' could not be found for CartsController
\config\routes.rb
Depot::Application.routes.draw do
root 'store#index', as: 'store'
resources :line_items
resources :carts
get "store/index"
resources :products
end
Depends on what is currently set in your #cart variable, can you put out an inspect for that?
Otherwise, you can also try something like this:
<%= link_to 'Empty Cart', cart_path(#cart), method: :delete, confirm: 'are you sure?' %>

undefined method `voted_on?' for nil:NilClass error message using thumbs_up

I'm currently trying to implement likes and unlikes rails app using thumbs_up. I followed the instructions on this page: Clarification on how to use "thumbs_up" voting gem with Rails 3
Users can like and unlike books. I have 2 buttons, like and unlike and I would like to hide one or the other from the user, depending on the users current like status. So I figured an if else would be appropriate like this:
<% if #user.voted_on?(#book) %>
<div class="unlike_button"><%= link_to("Unlike", unvote_book_path(book), method: :post) %></div>
<% else %>
<div class="like_button"><%= link_to("Like", vote_up_book_path(book), method: :post) %></div>
<% end %>
and in my route.rb file:
resources :books do
member do
post :vote_up
post :unvote
end
end
But when I run this I get the error message:
undefined method `voted_on?' for nil:NilClass
Is there anything I might be doing wrong?
Update
As Mischa suggested, I changed it to current_user.voted_on. Now I get this error message:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Below is a snippet of my Books controller
include UsersHelper
include SessionsHelper
before_filter :signed_in_user, only: [:index]
before_filter :admin_user, only: :destroy
def index
array = Book.search(params[:search])
#books = Kaminari.paginate_array(array).page(params[:page]).per(5)
end
def show
#book = Book.find(params[:id])
#respond_to do |format|
#format.js
#end
end
def destroy
Book.find(params[:id]).destroy
flash[:success] = "Book deleted."
redirect_to books_url
end
def vote_up
begin
current_user.vote_for(#book = Book.find(params[:id]))
flash[:success] = "Liked!."
redirect_to books_url
end
end
def unvote
begin
current_user.unvote_for(#book = Book.find(params[:id]))
flash[:success] = "Unliked!."
redirect_to books_url
end
end
You should change your method of link from post to put and
change your routes.rb to
resources :books
You can also update a data using a link by using this code:
<%= link_to "Like", book, :method => :put %>
or
<%= link_to "Unlike", book, :method => :put %>
and this goes with your controller in
assuming that you have a is_liked boolean field in your Post model
def update
#post = Post.find(params[:id])
if #post.is_liked?
#post.attributes = {
:is_liked => "f"
}
redirect_to posts_path, :flash => "Unliked"
else
#post.attributes = {
:is_liked => "t"
}
redirect_to posts_path, :flash => "Liked"
end
end

Rails: I want to add index to the resource :symbol route

Users have subscriptions to feeds but when I change (as suggested elsewhere on stackO)
resource :subscriptions
to
resources: subscriptions
it breaks some ajax functionality I already have implemented regarding destroy.
I want to be able to link to /subscriptions/ and have users be able to view all of their subscriptions. The problem is that currently it's bringing me to subscriptions#show when I really want #index.
How should I do this?
Here's my AJAX:
<div id="subscribe" class="shadow">
<% if session[:read_random]
unless is_subscribed?(session[:read_random].last)%>
<%= link_to 'Subscribe', subscriptions_path(feed_id: session[:read_random].last), method: :post, remote: :true %>
<% else %>
<%= link_to 'Unsubscribe', subscriptions_path(feed_id: session[:read_random].last), method: :delete, remote: :true %>
<% end
end %>
</div>
Here's my destroy method
def destroy
if params[:feed_id]
#this is the ajax call
#subscription = Subscription.find_by_user_id_and_feed_id(session[:user_id], params[:feed_id])
else
#this is the index destroy call (unsubscribe)
#subscription = Subscription.find(params[:id])
end
#subscription.destroy
respond_to do |format|
format.html { redirect_to request.referer }
format.js
format.json { head :no_content }
end
end
Using the default resourceful routes for a plural resource, the proper destroy route would be subscription_path(id), method: :delete. Note that subscription is singular. For these routes, the destroy path is generally identical to the show and update path, with the exception of http method used.

Resources