Search code examples
ruby-on-railsruby-on-rails-5

nocontent returned from controller action that calls helper rendering a partial


I am basically trying to use a helper method that renders a partial in my controller and getting a nocontent errors or 500 errors "no template" depending on the variations I'm trying. If I just run the code in the helper directly from the controller action, all is well. It seems like the rails guides don't touch on this (using a helper method that does the rendering, as opposed to directly from the controller action). Unfortunately, I don't have the luxury right now to refactor it out because the helper is used in other places, and it seems to me if there is a way to use this helper in the controller action also, that's the better option.

Anyone know how to do this? Nothing I am trying is working for me :S

Helper method

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

Controller action

# 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

Solution

  • based on your last example code, there is render :partial => that works

    I think it could be refactored like this

    # 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