title: Active Model Serializers : undefined method `url_for' for nil:NilClass
body: <p>I'm using <code>active_model_serializers</code> gem in my application to send high level json response. It does actually works fine but since I've install that gem, after few minutes of use, the application crash, showing the error above.</p>
  
  <p>Not sure if my code is related to it but somehow, I needed to send back some partial as well. Since it didn't seems to be supported by the gem, I did a work around :  </p>
  
  <pre><code>class AppSerializer &lt; ActiveModel::Serializer
  
    def render_partial_to_json(options = {})
      partial = options[:partial] || nil
      locals  = options[:locals] || nil
  
      context = Rails.configuration.paths['app/views']
      view    = ActionView::Base.new(context)
  
      view.class.class_eval do
        include Rails.application.routes.url_helpers
        include ApplicationHelper
      end
  
      view.render(
        partial: partial,
        locals: locals
      )
    end
  end
  </code></pre>
  
  <p>Then, in my serializer, I can do : </p>
  
  <pre><code>class ActivitySerializer &lt; AppSerializer
  
    attributes :id, :kind, :data, :created_at, :html
  
    has_many :comments
    has_one :user
  
    def id
      ScatterSwap.hash(object.id)
    end
  
    def html
      render_partial_to_json(
        partial: 'activities/post',
        locals: { activity: object }
      )
    end
  end
  </code></pre>
  
  <p>This code works fine, and I'm getting my partial as JSON attributes.
  The weird thing is that the error appears progressively on any page. The only way to remove it is by restarting the server. But again, after few minutes of use, the problem comes back.  </p>
  
  <p>Here is a part of the log :  </p>
  
  <pre><code>Completed 500 Internal Server Error in 65ms
  
  NoMethodError - undefined method `url_for' for nil:NilClass:
  actionpack (4.0.3) lib/action_dispatch/routing/url_for.rb:155:in `url_for'
  actionpack (4.0.3) lib/action_view/routing_url_for.rb:83:in `url_for'
  turbolinks (2.2.1) lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:234:in `call'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:178:in `call'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:274:in `block (2 levels) in define_url_helper'
  app/views/activities/_post.html.haml:2:in `_app_views_activities__post_html_haml__1000109198282705156_2214339020'
  actionpack (4.0.3) lib/action_view/template.rb:143:in `block in render'
  activesupport (4.0.3) lib/active_support/notifications.rb:161:in `instrument'
  actionpack (4.0.3) lib/action_view/template.rb:141:in `render'
  actionpack (4.0.3) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
  actionpack (4.0.3) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
  actionpack (4.0.3) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (4.0.3) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.3) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.3) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionpack (4.0.3) lib/action_view/renderer/partial_renderer.rb:278:in `render'
  actionpack (4.0.3) lib/action_view/renderer/renderer.rb:47:in `render_partial'
  actionpack (4.0.3) lib/action_view/helpers/rendering_helper.rb:27:in `render'
  haml (4.1.0.beta.1) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
  haml (4.1.0.beta.1) lib/haml/helpers.rb:89:in `non_haml'
  </code></pre>
  
  <p>Any of you already face something similar?  </p>
  
  <p><strong>EDIT :</strong><br>
  As requested, here is the code of my view :  </p>
  
  <pre><code>%article#comment-id-1.comment-item.m-t
  
  
  = link_to profile_url(activity.user), class: "pull-left thumb-sm avatar" do
    = display_picture_for activity.user, resizing_by: "36x36"
    %span.arrow.left
    %section.comment-body.panel.panel-default
      %header.panel-heading.bg-white
        = link_to profile_url(activity.user) do
          = activity.user.fullname
        %span.text-muted.m-l-sm.pull-right
          %i.fa.fa-clock-o
          = time_ago_in_words activity.created_at
      .panel-body
        %div
          = activity.data[:message]
        .comment-action.m-t-sm
          %a.btn.btn-default.btn-xs.active{"data-toggle" =&gt; "class", href: "#"}
            %i.fa.fa-star-o.text-muted.text
            %i.fa.fa-star.text-danger.text-active
            Like
          %a.btn.btn-default.btn-xs{href: "#comment-form"}
            %i.fa.fa-mail-reply.text-muted
            Reply
  </code></pre>
  
  <p><strong>EDIT 2 :</strong>  </p>
  
  <p>After digging a bit more, I found out that this code :  </p>
  
  <pre><code>class AppSerializer &lt; ActiveModel::Serializer
  
    def render_partial_to_json(options = {})
      partial = options[:partial] || nil
      locals  = options[:locals] || nil
  
      context = Rails.configuration.paths['app/views']
      view    = ActionView::Base.new(context)
  
      view.class.class_eval do
        include Rails.application.routes.url_helpers
        include ApplicationHelper
      end
  
      view.render(
        partial: partial,
        locals: locals
      )
    end
  end
  </code></pre>
  
  <p>when calling is overriding Rails helpers and loading libraries, which cause the entire system to be unworking. However, how can I fix it?</p>