I am building a simple admin page and I can't get a button to delete the user from the record and refresh the page. Confusingly, the button will make a POST request instead of a DELETE request so my "toggle_admin" function will trigger in my controller instead. I have the route set up correctly to handle a delete request so I am stumped.
index.html.erb
<th><%= button_to "Toggle", :method=> :post, params: {user_id: user.id} %></th>
<th><%= button_to "Destroy", :method=> :delete, params: {user_id: user.id} %></th>
pages_controller.rb
def toggle_admin()
selected = User.find(params[:user_id])
puts selected
selected.admin = !selected.admin
selected.save
redirect_to "/pages/admin"
flash[:success] = "User admin rights sucessfully toggled"
end
def destroy
selected = User.find(params[:user_id])
selected.destroy
redirect_to "/pages/admin"
end
routes.rb
get '/pages/admin', to: "pages#index"
post '/pages/admin', to: "pages#toggle_admin"
delete '/pages/admin', to: "pages#destroy"
Console (When clicking the button to destroy)
Started POST "/pages/admin?id=1&method=delete" for #### at 2022-09-19 14:55:06 -0700
Processing by PagesController#toggle_admin as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"1", "method"=>"delete"}
Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms | Allocations: 933)
To solve:
I needed to change the route to
delete '/pages/admin', to: "pages#destroy", :as => :user
And the button to:
<th><%= button_to "Destroy", user, :method => :delete, params: {user_id: user.id} %></th>
Related
Hello fellow programmers, I have been working on a order management system. I finally have worked out all my errors except for one i cant get out. Once i delete a customer or a order i get a error message saying "Routing error".
Routing Error No route matches [POST] "/customers/2"
Rails.root: /Users/cecil/Desktop/order_management_systeem
Application Trace | Framework Trace | Full Trace Routes
This is my route
Prefix Verb URI Pattern Controller#Action
customer_orders GET /customers/:customer_id/orders(.:format) orders#index
POST /customers/:customer_id/orders(.:format) orders#create
new_customer_order GET /customers/:customer_id/orders/new(.:format) orders#new
edit_customer_order GET /customers/:customer_id/orders/:id/edit(.:format) orders#edit
customer_order GET /customers/:customer_id/orders/:id(.:format) orders#show
PATCH /customers/:customer_id/orders/:id(.:format) orders#update
PUT /customers/:customer_id/orders/:id(.:format) orders#update
DELETE /customers/:customer_id/orders/:id(.:format) orders#destroy
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PATCH /customers/:id(.:format) customers#update
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
GET /:controller/:action/:id/:customer_id(.:format) :controller#:action
this is my erb code
<%= link_to("Delete", customer_path(#customer), method: :delete, confirm: "Are you sure?", :class => 'action delete') %>
Destroy controller
def destroy
customer = Customer.find(params[:id]).destroy
flash[:notice] = "Subject '#{customer.first_name}' destroyed successfully"
redirect_to(:action => 'index')
end
The action destroy should look as something along the following lines:
def destroy
#customer = Customer.find(params[:id]
#customer.destroy
redirect_to(
customers_path,
notice: 'Customer successfully deleted'
)
end
Also edits to link:
<%= link_to("Delete", #customer, method: :delete, data: { confirm: "Are you sure?" }) %>
The problem in your case, is that you've defined variable customer, but use #customer in your url_helper (customer_path(#customer)).
Another thing (more important), is that you actually assign a customer a value, which is the result of deleting an object from database:
customer = Customer.find(params[:id]).destroy
Never do so.
Either do
Customer.find(params[:id]).destroy
or
#customer = Customer.find(params[:id])
#customer.destroy
I have a logout link that should be routing to users#do_logout but no matter what I do, if I click the link, it routes to the users#show. Here is the code:
Route:
resources :users do
member do
get :profile
post :profile
end
collection do
get "signup", to: 'users#new'
get "login"
post "do_login"
post "do_logout"
end
end
Link:
li = link_to "Sign Out", do_logout_users_path
Users controller action:
def do_logout
session[:user_id] = nil
redirect_to :root
end
Any help would be much appreciated. This is driving me insane.
Your code is not working because, you have set up a POST route for do_logout and your Sign Out link is making a GET request.
To do a POST request from the view, you have to create a form
= form_tag do_logout_users_path do
= submit_tag 'Sign Out'
OR
You could use delete method for this
in routes
delete "do_logout"
and the link
= link_to "Sign Out", do_logout_users_path, :method => :delete
I was originally getting a routing error posted in Routing Errors on attempts to delete all records in a table using Rails 4 through a link_to, and that has been resolved.
Now I am having problems with "id=remove_all" being passed. I'm using Rails 4, and am trying to delete all the assessments using a link_to helper. The pertinent code is below:
routes.rb
resources :assessments do
collection do
delete :remove_all
end
end
assessment index.html.erb
<p>To delete all assessments in one swoop, click <%= link_to 'Remove ALL Assessments', remove_all_assessments_path, method: :destroy, data: { confirm: 'Are you sure?' } %>
assessments_controller.rb
def remove_all
#assessments = Assessment.all
#assessments.each do |assessment|
assessment.destroy(assessment.id)
end
flash[:notice] = "All assessments have been deleted."
redirect_to assessments_url
end
I run rake routes and
Prefix Verb URI Pattern Controller#Action
remove_all_assessments DELETE /assessments/remove_all(.:format) assessments#remove_all
The HTML source generated for the link:
<p>To delete all assessments in one swoop, click <a data-confirm="Are you sure?" data-method="destroy" href="/assessments/remove_all" rel="nofollow">Remove ALL Assessments</a>
When I click on the 'Remove All Assessments' link, I expect to have the remove_all action run in the AssessmentsController, and delete all the assessments in #assessments, then redirect me to the assessments_url. However, when I click on the 'Remove ALL Assessments', link, I am brought to the url: http://localhost:3000/assessments/remove_all
ActiveRecord::RecordNotFound in AssessmentsController#destroy
Couldn't find Assessment with id=remove_all
# Use callbacks to share common setup or constraints between actions.
def set_assessment
#assessment = Assessment.find(params[:id])
end
The logs show:
Started DELETE "/assessments/remove_all" for 127.0.0.1 at 2014-01-31 15:41:41 -0500
Processing by AssessmentsController#destroy as HTML
Parameters: {"authenticity_token"=>"fooBarBaz=", "id"=>"remove_all"}
How do pass the collection of #assessments to the remove_all action using the Delete verb? Is this possible?
It is Because
resources :assessments
already have route to destroy
like /assessment/:id with method :delete
so your remove_all_assessments_path will hit to that url bcoz it comes first in in your routes.rb file
To solve your problem u have to do something like this:-
resources :assessments, except: [:destroy] do
collection do
delete :remove_all
end
end
or define your remove_all_assessments url above it.
like
match "/assessments/remove_all", to: "assessments#remove_all", via: [:delete], as: :remove_all_assessments
resources :assessments do
collection do
delete :remove_all
end
end
I want to have an AJAX call to a destroy action in my application, and am following example #3 of the guide Ajax in Rails 3.1 - A Roadmap to do this. I'm using Rails 3.2.8.
i got the AJAX request working just fine, and the destroy action is called just as I expect it to.
When a user requests the HTML page of the destroy action, I want to delete the item, and then redirect her to another page.
The problem is that both the HTML and AJAX requests stop working when I insert code to redirect. When the code is there, an AJAX request returns 500 Internal Server Error, and an HTML request returns "The action 'show' could not be found for SafetyTestsController". When the two lines are not there, AJAX works fine and HTML still returns the same error.
View (Unrendered):
<%= link_to 'Delete', {controller: 'safety_tests', action: 'destroy', id: safety_test.id}, remote: true, method: :delete, confirm: 'Are you sure?', id: "delete_safety_test" %>
View (Rendered):
Delete
app/controllers/safety_tests_controller.rb
class SafetyTestsController < ApplicationController
respond_to :html, :js
...
def destroy
test = SafetyTest.find(params[:id])
student_id = test.student_id
test.destroy
respond_with(test) do |format|
format.js { render nothing: true }
end
#Two lines below should run for an HTML request but not a Javascript request.
#When uncommented, AJAX request returns 500 Internal Server Error
#flash[:notice] = 'Safety test deleted successfully.'
#redirect_to controller: 'students', action: 'show', id: student_id
end
rake routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
root / dashboard#home
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
trips GET /trips(.:format) trips#index
POST /trips(.:format) trips#create
new_trip GET /trips/new(.:format) trips#new
edit_trip GET /trips/:id/edit(.:format) trips#edit
trip GET /trips/:id(.:format) trips#show
PUT /trips/:id(.:format) trips#update
DELETE /trips/:id(.:format) trips#destroy
safety_tests GET /safety_tests(.:format) safety_tests#index
POST /safety_tests(.:format) safety_tests#create
new_safety_test GET /safety_tests/new(.:format) safety_tests#new
edit_safety_test GET /safety_tests/:id/edit(.:format) safety_tests#edit
safety_test GET /safety_tests/:id(.:format) safety_tests#show
PUT /safety_tests/:id(.:format) safety_tests#update
DELETE /safety_tests/:id(.:format) safety_tests#destroy
medical_forms GET /medical_forms(.:format) medical_forms#index
POST /medical_forms(.:format) medical_forms#create
new_medical_form GET /medical_forms/new(.:format) medical_forms#new
edit_medical_form GET /medical_forms/:id/edit(.:format) medical_forms#edit
medical_form GET /medical_forms/:id(.:format) medical_forms#show
PUT /medical_forms/:id(.:format) medical_forms#update
DELETE /medical_forms/:id(.:format) medical_forms#destroy
parent_permission_forms GET /parent_permission_forms(.:format) parent_permission_forms#index
POST /parent_permission_forms(.:format) parent_permission_forms#create
new_parent_permission_form GET /parent_permission_forms/new(.:format) parent_permission_forms#new
edit_parent_permission_form GET /parent_permission_forms/:id/edit(.:format) parent_permission_forms#edit
parent_permission_form GET /parent_permission_forms/:id(.:format) parent_permission_forms#show
PUT /parent_permission_forms/:id(.:format) parent_permission_forms#update
DELETE /parent_permission_forms/:id(.:format) parent_permission_forms#destroy
teacher_permission_forms GET /teacher_permission_forms(.:format) teacher_permission_forms#index
POST /teacher_permission_forms(.:format) teacher_permission_forms#create
new_teacher_permission_form GET /teacher_permission_forms/new(.:format) teacher_permission_forms#new
edit_teacher_permission_form GET /teacher_permission_forms/:id/edit(.:format) teacher_permission_forms#edit
teacher_permission_form GET /teacher_permission_forms/:id(.:format) teacher_permission_forms#show
PUT /teacher_permission_forms/:id(.:format) teacher_permission_forms#update
DELETE /teacher_permission_forms/:id(.:format) teacher_permission_forms#destroy
trip_deposits GET /trip_deposits(.:format) trip_deposits#index
POST /trip_deposits(.:format) trip_deposits#create
new_trip_deposit GET /trip_deposits/new(.:format) trip_deposits#new
edit_trip_deposit GET /trip_deposits/:id/edit(.:format) trip_deposits#edit
trip_deposit GET /trip_deposits/:id(.:format) trip_deposits#show
PUT /trip_deposits/:id(.:format) trip_deposits#update
DELETE /trip_deposits/:id(.:format) trip_deposits#destroy
trip_fees GET /trip_fees(.:format) trip_fees#index
POST /trip_fees(.:format) trip_fees#create
new_trip_fee GET /trip_fees/new(.:format) trip_fees#new
edit_trip_fee GET /trip_fees/:id/edit(.:format) trip_fees#edit
trip_fee GET /trip_fees/:id(.:format) trip_fees#show
PUT /trip_fees/:id(.:format) trip_fees#update
DELETE /trip_fees/:id(.:format) trip_fees#destroy
team_dues GET /team_dues(.:format) team_dues#index
POST /team_dues(.:format) team_dues#create
new_team_due GET /team_dues/new(.:format) team_dues#new
edit_team_due GET /team_dues/:id/edit(.:format) team_dues#edit
team_due GET /team_dues/:id(.:format) team_dues#show
PUT /team_dues/:id(.:format) team_dues#update
DELETE /team_dues/:id(.:format) team_dues#destroy
How can I get the two lines to work?
respond_to do |format|
format.html { redirect_to(url_for(controller: 'students', action: 'show', id: student_id), notice: 'Safety test deleted successfully.') } # actually can be without url_for
format.js { render nothing: true }
end
The second reason can be that HTML request doesn't contain DELETE method, so ActionDispatch thinking you request to :action => 'show' in SafetyTestsController. Check it.
<%= link_to 'Delete', {controller: 'safety_tests', action: 'destroy', id: safety_test.id}, remote: true, method: :delete, :data => { confirm: 'Are you sure?' }, id: "delete_safety_test" %>
Why don't you use named routes?:
<%= link_to 'Delete', safety_test, remote: true, method: :delete, :data => { confirm: 'Are you sure?' }, id: "delete_safety_test" %>
Found the answer. This is the code that works for both JS and HTML:
<%= form_for(safety_test, method: :delete, remote: true, html: {class: "delete_safety_test"}) do |f| %>
<%= f.submit "Delete", confirm: "Are you sure?" %>
<% end %>
I am trying to build a friendship system sorta following this link: How to Implement a Friendship Model in Rails 3 for a Social Networking Application?. However lack a bit. I was able to create a relationship however i am not to sure on how to perform the following actions: cancel, decline, accept.
So lets say i try to cancel the relationship i do the following on the pending, to call the actions i do the follow:
<% #customer.pending_friends.each do |pf| %>
<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :post %><br />
<% end %>
Here the controller of cancel
def cancel
#customer = current_customer
#friend = Customer.find(params[:friend_id])
if #customer.pending_friends.include?(#friend)
Friendship.breakup(#customer, #friend)
flash[:notice] = "Friendship Canceled"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
and here my breakup function
# Delete a friendship or cancel a pending request.
def self.breakup(customer, friend)
transaction do
destroy(find_by_customer_id_and_friend_id(customer, friend))
destroy(find_by_customer_id_and_friend_id(friend, customer))
end
end
I am however getting a no route errors when clicking on the cancel links. What i am doing wrong??
Here on the request
route.rb
resources :friendships do
collection do
get 'cancel'
get 'decline'
end
end
resources :friendships
rake routes
cancel_friendships GET /friendships/cancel(.:format) friendships#cancel
decline_friendships GET /friendships/decline(.:format) friendships#decline
GET /friendships(.:format) friendships#index
POST /friendships(.:format) friendships#create
GET /friendships/new(.:format) friendships#new
GET /friendships/:id/edit(.:format) friendships#edit
GET /friendships/:id(.:format) friendships#show
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
/********************************************************/
friendships GET /friendships(.:format) friendships#index
POST /friendships(.:format) friendships#create
new_friendship GET /friendships/new(.:format) friendships#new
edit_friendship GET /friendships/:id/edit(.:format) friendships#edit
friendship GET /friendships/:id(.:format) friendships#show
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
The problem is that in your routes you have:
get 'cancel'
but your cancel-link is doing a post request, not a get:
<%= link_to ..., ..., :method => :post %>
Personally I think it should be a delete request.
In your routes:
delete 'cancel'
In your view:
<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :delete %>
Your code may have other problems, but this is one thing you have to fix.