【问题标题】:Index controller action is not executed and view not rendered未执行索引控制器操作且未呈现视图
【发布时间】:2018-09-06 17:40:03
【问题描述】:
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

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

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 索引视图,并且需要刷新浏览器才能发生这种情况?

【问题讨论】:

  • 看起来浏览器正在使用 DELETE 方法而不是 GET 进行重定向,这就是它失败的原因。虽然不知道如何改变它,但有一点开始调试/搜索。

标签: ruby-on-rails


【解决方案1】:

重定向时需要指定状态: 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 选项

【讨论】:

  • 这有点工作。重定向现在正确完成,但通知或警报没有显示索引视图呈现
  • 编辑了答案 ;)
  • status是在中间还是在最后,好像都无所谓了。操作正确完成,但仍然没有通知或警报
  • 你能用_path代替_url吗?这可能是它。在那之后我没有想法了:O
  • 请看我的问题。我使用的是 _path 而不是 _url,虽然我不认为它会有所作为。
猜你喜欢
  • 2018-07-10
  • 2012-10-20
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2012-02-05
  • 2016-06-10
相关资源
最近更新 更多