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?' %>
Related
Trying to get delete action for topics controller to work properly.
Here is my topics controller
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path(#topic)
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
My routes:
resources :topics do
resources :bookmarks, except: [:index]
end
My delete link on my index view:
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
Really don't understand what I am missing.
Try with these changes:
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
Delete Link:
<%= link_to "Delete Topic", topic_path(#topic), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
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
I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy
To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!
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
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