nocontent 从调用 helper 渲染部分的控制器操作返回

我基本上是在尝试使用辅助方法在我的控制器中呈现部分并根据我尝试的变体获得 nocontent 错误或 500 个“无模板”错误。如果我直接从控制器操作中运行助手中的代码,一切都很好。似乎 rails guides 没有涉及到这个(使用辅助方法进行渲染,而不是直接从控制器动作)。不幸的是,我现在没有能力重构它,因为助手在其他地方使用,而且在我看来,如果有一种方法也可以在控制器操作中使用这个助手,那是更好的选择。

有人知道怎么做吗?我正在尝试的任何东西都不适合我:S

辅助方法

def render_call_orders_filters url_or_path_method,query_params = @query_params
    render :partial => 'call_orders/call_orders_filters',:locals => {url_or_path_method: url_or_path_method,query_params: query_params}
end

控制器动作

# GET /call_order/filters
def filters
  respond_to do |format|
    format.html {
      # This (of course) works...
      # render :partial => 'call_orders/call_orders_filters',\
      #  :locals => { url_or_path_method: method(:call_orders_path),query_params: @query_params }
      # This does not
      helpers.render_call_orders_filters(method(:call_orders_path))
      
      # This gives me a "Template is missing" 500 error
      renter text: helpers.render_call_orders_filters(method(:call_orders_path))
    }
  end
end
dfasdfewrt 回答:nocontent 从调用 helper 渲染部分的控制器操作返回

根据您的上一个示例代码,render :partial => 有效

我认为它可以像这样重构

# The helper...
def orders_filters_param_to_template(url_or_path_method,query_params = {})
  {
    partial: 'call_orders/call_orders_filters',locals: { url_or_path_method: url_or_path_method,query_params: query_params }
  }
end

def render_call_orders_filters(url_or_path_method,query_params = @query_params)
  template_data = orders_filters_param_to_template(url_or_path_method,query_params)
  render partial: template_data[:partial],locals: template_data[:locals]
end

# The controller action...
def filters
  respond_to do |format|
    format.html do
      template_data = helpers.orders_filters_param_to_template(method(:call_orders_path),@query_params)

      render partial: template_data[:partial],locals: template_data[:locals]
    end
  end
end
本文链接:https://www.f2er.com/2066.html

大家都在问