Reputation: 6639
Rails 3.2
In my admin/tickets_controllers.rb, I have the following:
def index
params[:direction] ||= "desc"
params[:sort] ||= "requested_date_start"
@tickets = Ticket.solr_search( include: [:customer_info, :customer, :executor, :notes] ){
fulltext params[:query] if params[:query].present?
with :status, (params[:status] || params[:filter]) if (params[:status] || params[:filter]).present?
order_by( params[:sort], params[:direction] ) if params[:sort].present? && params[:direction].present?
paginate :page => params[:page], per_page: params[:per_page]
}.results
render partial: 'tickets' if request.xhr?
end
def destroy
@ticket = Ticket.find params[:id]
existing_workflow_state = @ticket.workflow_state
msg_success = "Ticket #{@ticket.number} status has been changed from #{existing_workflow_state} to cancelled"
msg_already_cancelled = "Ticket #{@ticket.number} is already cancelled"
msg_failure = "Ticket #{@ticket.number} status is #{@ticket.workflow_state}, it cannot be cancelled"
if existing_workflow_state == 'cancelled'
redirect_to admin_tickets_path, alert: "Ticket #{@ticket.number} has already been cancelled"
elsif !ticket_can_be_deleted(@ticket).nil?
@ticket.workflow_state = 'cancelled'
@ticket.save
redirect_to admin_tickets_path, notice: "Ticket #{@ticket.number} status has been changed from #{existing_workflow_state} to cancelled"
else
redirect_to admin_tickets_path, alert: "Ticket #{@ticket.number} status is #{existing_workflow_state}, it cannot be cancelled"
end
end
private
def ticket_can_be_deleted(ticket)
w = ticket.workflow_state
if (w == 'closed') || (w == 'completed') || (w == 'submit_for_billing') || (w == 'needs_correction') || (w == 'cancelled')
return nil
else
return true
end
end
The following is the route:
admin_tickets GET /admin/tickets(.:format) admin/tickets#index
However, when I execute the Destroy action, nothing happens. The log file tells me:
Started DELETE "/admin/tickets/177685" for xx.xxx.xxx.x at 2018-09-06 16:58:53 +0000
Processing by Admin::TicketsController#destroy as */*
Parameters: {"id"=>"177685"}
......
Redirected to http://test.myapp.com/admin/tickets
Completed 302 Found in 43.8ms (ActiveRecord: 30.9ms)
Started DELETE "/admin/tickets" for xx.xxx.xxxx.xx at 2018-09-06 16:58:54 +0000
Processing by ApplicationController#routing_error as */*
Parameters: {"unmatched_route"=>"admin/tickets"}
....
Completed 404 Not Found in 14.0ms
ActionController::RoutingError (No route matches admin/tickets):
app/controllers/application_controller.rb:129:in `routing_error'
app/middleware/catch_json_parse_errors.rb:8:in `call'
But, if I hit the browser refresh button, immediately after, I get the tickets Index, with the following alert on top:
Ticket 177685 has already been cancelled
And the log file has in it:
Started GET "/admin/tickets" for xx.xxx.xxx.x at 2018-09-06 17:05:07 +0000
Processing by Admin::TicketsController#index as HTML
.....
Rendered shared/_index_search_form.html.slim (0.8ms)
Rendered admin/tickets/_ticket.html.slim (87.5ms)
Rendered shared/_no_result.html.slim (0.0ms)
Rendered admin/tickets/index.html.slim within layouts/application (95.8ms)
Completed 200 OK in 140.2ms (Views: 101.5ms | ActiveRecord: 14.7ms | Solr: 4.0ms)
Any ideas why the admin/tickets Index view is not rendered, and needs a browser refresh for that to happen?
Upvotes: 0
Views: 196
Reputation: 2365
You need to specify the status when redirecting:
redirect_to ..., status: 303
Explanations in a Redirect_to from Destroy action always gets DELETE verb whatever :method I declare and https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to
The documentation says the following:
It is also possible to assign a flash message as part of the redirection. There are two special accessors for the commonly used flash names
+alert+ and +notice+ as well as a general purpose +flash+ bucket.
redirect_to post_url(@post), alert: "Watch it, mister!"
redirect_to post_url(@post), status: :found, notice: "Pay attention to the road"
redirect_to post_url(@post), status: 301, flash: { updated_post_id: @post.id }
redirect_to({ action: 'atom' }, alert: "Something serious happened")
So it is possible to specify both status
and notice
options. However, make sure to put them in the same order as the doc.
If that still fails for you, you can try using the flash
option instead
Upvotes: 2