Link_to url helper not working in render_to_string for Rails 4

I'm trying to get link_to to work when rendering a view outside of a controller. I have tried a couple of different solutions found online. You may notice that one attempt is commented out.

The result is: undefined `host 'method for nil: NilClass

    ac = ApplicationController.new
    ac.class.include Rails.application.routes.url_helpers

    #context = Rails.configuration.paths['app/views']
    #view = ActionView::Base.new(context)
    #view.class.include Rails.application.routes.url_helpers
    #view.render :layout => 'layouts/pdf.html.haml', :template => 'reports/show.pdf.haml', locals: {alerts:@alerts}

    ac.render_to_string(:layout => 'layouts/pdf.html.haml', :template => 'reports/show.pdf.haml', locals: {alerts:@alerts})

      

I have also tried riding in class. None of the ActionView :: Base controls work.

class ViewRenderer < ActionView::Base
  include Rails.application.routes.url_helpers
  include ApplicationHelper

  def default_url_options
     {host: Rails.application.routes.default_url_options[:host]}
  end
end

      

+3
ruby-on-rails-4


source to share


No one has answered this question yet

Check out similar questions:

596
How to use problems in Rails 4
416
Rails 4: how to use $ (document) .ready () with turbo links
385
Rails I18n validation warning warning
328
Rails 4: before_filter vs. before_action
313
Rails update_attributes without saving?
310
Rails 4: list of available datatypes
247
How is attr_accessible used in Rails 4?
0
Data attribute with variable not working with link_to rails helper
0
How to show all Users who liked my post
0
ActionMailer class does not inherit in Rails application



All Articles
Loading...
X
Show
Funny
Dev
Pics