12

How to render HTML using a Ruby on Rails API application 5

 2 years ago
source link: https://www.codesd.com/item/how-to-render-html-using-a-ruby-on-rails-api-application-5.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to render HTML using a Ruby on Rails API application 5

advertisements

We're using Ruby on Rails 5.0.0.1 in API-Mode. What Middleware and Configurations do I need to add, in order to being able to render html instead of json in an controller.

Edit:

Thank you all for your answers.

Want I wanted is to render normal erb/haml views. not html in json like 'kartik upadhyay' mentioned. Since the Application is primary an JSON API we didn't want the full blown default Rails Installation, which mean our Application Controller extends from ActionController::API.

My Plan was to make a PdfRendering Controller which includes all Modules needed for ActionView to work. What I rather did was reading the asset pipeline (because we wanted to use sass) and inherit the application controller from ActionController::Base.. as other rails apps would do.

It's not the nicest solution since you're including the whole ActionController::Base and all of it's Feature that you may not be using. But it's still slimmer that a full Blown Rails installation (especially the middleware aka. sessions etc.)


Put inside your action:

respond_to do |format|
   format.html
   format.xml
   format.json
end

http://guides.rubyonrails.org/action_controller_overview.html is a nice start. And maybe it's nice to reconsider use "API mode" if you're returning html.

best!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK