Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use view helpers with rails-api gem?

I use rails-api and rabl gems to make api application. I don't want to put a lot of logic into rabl views but if that I want to extract it into helpers. Rails-api gem doesn't include helpers support (and thats ok) so how I can enable helpers into my json views?(what middleware I need to include, which modules etc)

like image 608
freemanoid Avatar asked Feb 07 '13 14:02

freemanoid


1 Answers

In the controller that renders the view you have to

  1. include ActionController::Helpers and
  2. provide your own Helper modules to the view using the 'helper' method

In my API application I created a RenderingController which acts as a superclass for all controllers that do render:

class RenderingController < ApplicationController
  include AbstractController::Layouts
  include AbstractController::Translation
  include ActionController::ImplicitRender
  include ActionController::Helpers

  helper ApplicationHelper, OtherHelper
end
like image 135
Andi Schacke Avatar answered Oct 15 '22 08:10

Andi Schacke