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
Related
My problem is that I need to add the option for delete product and I'm stock with that....
First I added this line in the index.html.erb:
<% glyph_to "Delete", product, method: :delete, data: (confirm: "Are you sure that product" ##(product.id) is not used?") if can? (:destroy, product)%>
but I don't now in that line what is the difference between "method: :delete" and "if can? (:destroy, product)", destroy is the controller action I guess and delete? I don't know....
another thing is my controller definition for that action:
def destroy
#product = Product.find(params[:id])
#product.destroy
redirect_to product_path
end
but when I press "delete" it doesn't redirect to product_path
and my routes definition are the following:
resources products do
member do
get :destroy
end
end
I will be very grateful of any help with this,
thank you for your time :D!
Yes , destroy is controller's delete method , to redirect to the product listing page you should use redirect_to action: 'index'. You can check all actions and method through command rake routes
You should redirect_to products_path(this translates to index) since currently it says redirect_to product path(translating to show) which will throw error
method: :delete is for posting to destroy action and can? is a cancan gem method by Ryan Bates. The if checks if user has the permissions to delete the product or not.
The permissions can be set in app/models/ability.rb
Lastly change your routes to:
resources: :products
destroy action must be called on HTTP DELETE request and never on GET. You should definitely fix the following code from your routes:
resources products do ## missing : here, it should be :products
## member block not required
member do
get :destroy ## destroy should never be GET request
end
end
You just need to define your routes as:
resources :products ## Notice :products and not products
This will generate following routes for you which you can check by running command rake routes
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
Notice the last route generated.
destroy action should be called as HTTP Delete method which is why on your link you need to specify method: :delete as
<% glyph_to "Delete", product, method: :delete, data: (confirm: "Are you sure that product" ##(product.id) is not used?") if can? (:destroy, product)%>
so when you click on this link a DELETE request would be sent which would be mapped to your route for products#destroy, i.e.,
DELETE /products/:id(.:format) products#destroy
Also, you need to update the destroy action as below:
def destroy
#product = Product.find(params[:id])
#product.destroy
redirect_to products_url ## products_url notice plural products
end
products_url would redirect you to index page of products.
As product_path is for show, i.e., to show a particular product. You can't show a deleted product that too without providing an id to product_path. So, logically you should be redirecting to index action with products_url (NOTE using **_url for redirection is advisable but you could also use products_path)
When submitting my form (_reply_form.html.erb) I get this error:
Routing Error
No route matches [POST] "/responses/replies/4"
Try running rake routes for more information on available routes.
This is how my architecture is set up: Offering has many Responses. Response has many Replies.
I know I need the form to submit to this route, but I can't get it to do that:
response_replies POST /responses/:response_id/replies(.:format) replies#create
_reply_form.html.erb:
<%= form_for(#reply, :url => response_reply_path([#response, #reply])) do |f| %>
<%= render 'common/form_errors', object: #reply %>
<%= f.label :body, "Your Reply" %>
<%= f.text_area(:body, :rows => 10, :class => "field span6") %>
<%= f.submit "Post Reply", :class => "block" %>
<% end %>
_response.html.erb:
<%= render 'replies/reply_form', {:response => #response, :reply => #reply} %>
offerings/show.html.erb:
<% if #offering.responses.any? %>
<%= render #offering.responses %>
<% else %>
<p>This offering has no responses yet.</p>
<% end %>
responses_controller.rb:
class ResponsesController < ApplicationController
before_filter :auth, only: [:create]
def show
#offering = Offering.new
#response = Response.new
#reply = Reply.new
end
def create
#offering = Offering.find(params[:offering_id])
# now that we have our offering we use it to
# build a response with it
#response = #offering.responses.build(params[:response])
# now we get the user who posted the response
#response.user = current_user
if #response.save
flash[:success] = 'your response has been posted!'
redirect_to #offering
else
#offering = Offering.find(params[:offering_id])
render 'offerings/show'
end
end
end
routes.rb:
resources :offerings, except: [:new] do
# makes it easier for us to display
# forms for responses on the offering show page
# allows us to have access to the
# offering that the response is associated to
resources :responses, only: [:create]
end
resources :responses, except: [:new] do
resources :replies, only: [:create]
end
rake routes produces this:
root / dashboard#index
users POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
need_applicants GET /needs/:need_id/applicants(.:format) applicants#index
POST /needs/:need_id/applicants(.:format) applicants#create
new_need_applicant GET /needs/:need_id/applicants/new(.:format) applicants#new
edit_need_applicant GET /needs/:need_id/applicants/:id/edit(.:format) applicants#edit
need_applicant GET /needs/:need_id/applicants/:id(.:format) applicants#show
PUT /needs/:need_id/applicants/:id(.:format) applicants#update
DELETE /needs/:need_id/applicants/:id(.:format) applicants#destroy
needs GET /needs(.:format) needs#index
POST /needs(.:format) needs#create
edit_need GET /needs/:id/edit(.:format) needs#edit
need GET /needs/:id(.:format) needs#show
PUT /needs/:id(.:format) needs#update
DELETE /needs/:id(.:format) needs#destroy
offering_responses POST /offerings/:offering_id/responses(.:format) responses#create
offerings GET /offerings(.:format) offerings#index
POST /offerings(.:format) offerings#create
edit_offering GET /offerings/:id/edit(.:format) offerings#edit
offering GET /offerings/:id(.:format) offerings#show
PUT /offerings/:id(.:format) offerings#update
DELETE /offerings/:id(.:format) offerings#destroy
response_replies POST /responses/:response_id/replies(.:format) replies#create
responses GET /responses(.:format) responses#index
POST /responses(.:format) responses#create
edit_response GET /responses/:id/edit(.:format) responses#edit
response GET /responses/:id(.:format) responses#show
PUT /responses/:id(.:format) responses#update
DELETE /responses/:id(.:format) responses#destroy
register /register(.:format) users#new
login /login(.:format) sessions#new
/offerings(.:format) offerings#index
/needs(.:format) needs#index
dashboard /dashboard(.:format) dashboard#index
contact /contact(.:format) contact#index
your_offerings /your_offerings(.:format) offerings#your_offerings
your_needs /your_needs(.:format) needs#your_needs
search /search(.:format) offerings#search
logout DELETE /logout(.:format) sessions#destroy
-- Original question
This should work for your form_for
<%= form_for([#response, #reply], :url => response_reply_path do |f| %>
-- Second part of your question route matches {:action=>"show", :controller=>"replies"}
I don't see anything wrong in your code, it's maybe in a part of your code you did not paste? Try to search for a link_to that would be corresponding
-- Bonus
Also you should not need to write your render with local variables, the controller instance variables #response and #reply will automatically be present in your partial
<%= render 'replies/reply_form' %>
# #response and #reply are automatically forwarded to all your views and partials
Off course if you are using response and reply in your partial you can rename them to #response and #reply respectively
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.
This is what I've used with remote_form_tag:
<% form_remote_tag(:url => {:controller => '/companies', :action => 'update'},
:update => 'tags') do %>
<%= text_field :company, :tag_list %>
<%= submit_tag 'Save' %>
<% end %>
This is in a Company.view, where Company is a model that is acts_as_taggable_on enabled.
My expectation is that, via ajax, a post is made to companies/10/update
But, instead, what is posted is:
http://localhost:3000/companies/10
and the response is:
No action responded to 10. Actions: create, destroy, edit, email_this_week, index, new, show, and update
This is the update method in CompaniesController:
def update
#company = Company.find(params[:id])
if request.xhr?
# add the given tag to the company
#company.tags << params[:company][:taglist]
#company.save
render :partial => 'tags'
else
if #company.update_attributes(params[:company])
flash[:notice] = "Successfully updated company."
redirect_to #company
else
render :action => 'edit'
end
end
end
Help...?
DELETE /companies/:company_id/contacts/:id(.:forma
{:controller=>"contacts", :action=>"destroy"}
companies GET /companies(.:format)
{:controller=>"companies", :action=>"index"}
POST /companies(.:format)
{:controller=>"companies", :action=>"create"}
new_company GET /companies/new(.:format)
{:controller=>"companies", :action=>"new"}
edit_company GET /companies/:id/edit(.:format)
{:controller=>"companies", :action=>"edit"}
company GET /companies/:id(.:format)
{:controller=>"companies", :action=>"show"}
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
DELETE /companies/:id(.:format)
{:controller=>"companies", :action=>"destroy"}
When you update a resource like Company with ID 10, Rails will use the RESTful route:
PUT /companies/10
The PUT method is taken into account when routing your request. Taken from your routes:
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
This is correct behaviour for Rails. Just implement the update method in your CompaniesController.
If you require more info on RESTful routes in Rails, check up on this document: http://guides.rubyonrails.org/routing.html