Routing Error No route matches [DELETE] "/topics" - ruby-on-rails

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?' } %>

Related

Rails - ActionController::UrlGenerationError No route matches missing required keys: [:id]

I'm getting the above error in my Rails app when attempting to implement the edit function to a Comments MVC process.
This is the views code where the error is arising -
_comments.html.erb
<% if user_signed_in? %>
<p><%= link_to "Edit", edit_event_comment_path(#event, comment), remote: true %></p>
<p><%= link_to 'Delete', [comment.event, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
<% end %>
This is the comments controller code -
Comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
def create
#event = Event.find(params[:event_id])
#comment = #event.comments.create!(params[:comment].permit(:name, :body))
#comment.user_id = current_user.id
redirect_to #event
end
# GET /comments/1/edit
def edit
respond_to do |f|
f.js
f.html
end
end
def update
respond_to do |format|
if #comment.update
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.js { }
format.json { render :show, status: :created, location: #comment }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#event = Event.find(params[:event_id])
#comment = #event.comments.find(params[:id])
#comment.destroy
redirect_to event_path(#event)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:event_id, :body)
end
end
Here are my routes -
I have comments set up as a nested route for events so the route path is correct. I've tried a few different variations on this as trial and error but I'm still getting errors. I'm sure the answer is staring me in the face but I need help from a different set of eyes. Any assistance appreciated.
According to your code, if you are not looping #comments,
comment should be #comment
<% if user_signed_in? %>
<p><%= link_to "Edit", edit_event_comment_path(#event, #comment), remote: true %></p>
<p><%= link_to 'Delete', [#comment.event, #comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
<% end %>
Also, change your edit action to,
def edit
#event = #comment.event
respond_to do |f|
f.js
f.html
end
end

Using Ajax requests in rails

Hi I want to build an app using Ajax. Whenever a user visits an other user page he can add him as a friend by clicking add friend link. This is my current code in users/show page
<% if current_user.friend?(current_user,#user) %>
<%= link_to "Unfriend", relation_path(#relation), user: #user, method: :delete, data: { confirm: 'Are you sure? This action cannot be undone' }, class: 'btn btn-default' %>
<% elsif #user != current_user %>
<%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post, class: 'btn btn-default' %>
<% end %>
My code in relations controller
def create
if params[:friend_id]
if Relation.where(user_id: current_user.id, friend_id: params[:friend_id]).present?
redirect_to user_path(params[:friend_id]), notice: "Already friends"
elsif current_user.id == params[:friend_id]
redirect_to user_path(params[:friend_id]), alert: "Invalid Request"
else
#relation = current_user.relations.new(friend_id: params[:friend_id], subject: Constants::Subject::MAKE)
if #relation.save
friend = User.find(params[:friend_id])
friend.relations.create!(friend_id: current_user.id, subject: Constants::Subject::MAKE)
redirect_to user_path(params[:friend_id]), notice: "Added as friend"
else
redirect_to user_path(params[:friend_id]), alert: "Could not add as friend"
end
end
else
redirect_to current_user, notice: "Invalid Request"
end
end
I am following this Ryan Bates rails casts. How can I make the link_to add friend Ajax request instead of html request. The current code is working fine. I want to add Ajax functionality for this. I have seen a lot of questions but I could not understand what exactly to be done. Please help.
Add remote: true to the link_to tag. There is more information in the guides abou rails and ajax. You will need a respond_to block to handle the response.
Start with adding remote: true and use the console to see the call being made and what the response is. Then you can figure out how to handle the response.
<%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post, remote: true, class: 'btn btn-default' %>
Please take a look:
Move your buttons into a partial
Render it inside a div with ID = "actions"
Update your controller response with format js
Add field create.js.erb which render your buttons by javascript
Render your flashes box by javascript by the same way (if needed)
your view_file.erb
<div id="actions">
<%= render "views/realtions/actions", relation: #relation, friend: #user %>
</div>
views/realtions/_actions.html.erb
<% if current_user.friend?(current_user, friend) %>
<%= link_to "Unfriend", relation_path(relation), user: friend, method: :delete, remote: true, data: { confirm: 'Are you sure? This action cannot be undone' }, class: 'btn btn-default' %>
<% elsif friend != current_user %>
<%= link_to "Add Friend", friendships_path(friend_id: friend.id), method: :post, remote: true, class: 'btn btn-default' %>
<% end %>
views/realtions/create.js.erb
<%- if #friend.present? %>
$("#actions").html("<%= j render 'views/realtions/actions', relation: #relation, friend: #friend %>")
<%- end %>
// render your flashes to DOM here!
controllers/relations_controller.rb
def create
if params[:friend_id]
#friend = User.find(params[:friend_id])
if Relation.where(user_id: current_user.id, friend_id: #friend.id).present?
flash[:notice] = "Already friends"
elsif current_user.id == params[:friend_id]
flash[:error] = "Invalid Request"
else
#relation = current_user.relations.new(friend_id: params[:friend_id], subject: Constants::Subject::MAKE)
if #relation.save
#friend.relations.create!(friend_id: current_user.id, subject: Constants::Subject::MAKE)
flash[:notice] = "Added as friend"
else
flash[:error] = "Could not add as friend"
end
end
else
flash[:notice] = "Invalid Request"
end
respond_to do |format|
format.js
end
end
Hope this help

Delete method in ROR route error when using remote true

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!

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?' %>

Ruby on Rails - Destroy Action returns 404

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

Resources