ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
208 stars 51 forks source link

Upgrading to 0.5.0 from 0.4.3 encountering error "undefined method `render' for RablRails:Module" #88

Open jiggneshhgohel opened 7 years ago

jiggneshhgohel commented 7 years ago

Hello,

In our Rail-based API-only app we are generating JSONs by rendering object directly.

There are cases when you want to render object outside Rails view context. For instance to render objects in the console or to create message queue payloads. For these situations, you can use RablRails.render as show below:

RablRails.render(object, template, :view_path => 'app/views', :format => :json) #=> "{...}

Recently we have started upgrading our Rails to 5.1.2 and as part of bundle update rabl-rails gem got updated to v 0.5.0 from 0.4.3 (bundle update output snippet)

Fetching rabl-rails 0.5.0 (was 0.4.3)
Installing rabl-rails 0.5.0 (was 0.4.3)

Now we are running our rspec and encountering following error:

     Failure/Error: RablRails.render(serializable_object, template.name, view_path: template.root_path_for_api_version.to_s, format: :json)

     NoMethodError:
       undefined method `render' for RablRails:Module

I compared the source-code and found that in v 0.4.3 RablRails::Renderer module exists, however in 0.5.0 tree it is removed

Found this commit https://github.com/ccocchi/rabl-rails/commit/12a3f0f72dca4ba2112398b26b80912462ab40c6 in which RablRails::Renderer module was removed.

Since that is removed what is the alternate approach to render the object directly? We are using this same approach of rendering the object directly across our API-only application. So it is very critical that we need this behaviour to be available.

Note: _Ours API application is built on Rails but not using rails built-in api-only features._

Please suggest the possible fixes for this removed behaviour.

jiggneshhgohel commented 7 years ago

@ccocchi can you please spare some time and provide your guidance on my problem above?

nimashariatian commented 7 years ago

+1 on this issue, would appreciate a ping @ccocchi

ccocchi commented 7 years ago

This module has been removed because it was trying to replicate too much ActionView internals for little gain and was poorly maintanable.

If you need the old behavior, you could try to copy https://github.com/ccocchi/rabl-rails/blob/v0.4.3/lib/rabl-rails/renderer.rb inside your application and either have RablRails extend it or call directly RablRails::Renderer.render as before.

nimashariatian commented 7 years ago

Thank for the update! what is new alternative to this @ccocchi :) also much appreciate the hard work

iovis commented 5 years ago

We've been using the following instead of RablRails::Renderer.render:

ApplicationController.render(
  template: 'api/v1/users/stats',
  assigns: { user: @user }
)
jiggneshhgohel commented 9 months ago

Using the suggestions in https://github.com/ccocchi/rabl-rails/issues/88#issuecomment-324875768 I was able to resolve my problem I described in https://github.com/ccocchi/rabl-rails/issues/88#issue-247669661 but I needed few customizations to be made which I have detailed in https://gist.github.com/jiggneshhgohel/1f0137e5dc294cc94141785eaf836c87.

Thanks