索引控制器操作未执行且视图未呈现

时间:2018-09-06 17:40:03

标签: ruby-on-rails

Rails 3.2

在我的admin / tickets_controllers.rb中,我具有以下内容:

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

以下是路线:

admin_tickets GET   /admin/tickets(.:format) admin/tickets#index

但是,当我执行“销毁”操作时,什么也没有发生。日志文件告诉我:

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'

但是,如果我点击浏览器刷新按钮,此后立即获得票证索引,并在顶部显示以下警报:

Ticket 177685 has already been cancelled

日志文件包含在其中:

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)     

有什么想法为什么不呈现admin / tickets索引视图,并且需要刷新浏览器才能实现?

1 个答案:

答案 0 :(得分:2)

您需要在重定向时指定状态: redirect_to ..., status: 303

a Redirect_to from Destroy action always gets DELETE verb whatever :method I declare中的说明  和https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

文档中的内容如下:

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")

因此可以同时指定statusnotice选项。但是,请确保按与文档相同的顺序放置它们。

如果仍然无法解决问题,您可以尝试使用flash选项