Rails responders and Layouts

145 Views Asked by At

Ok, so I have a Rails app, the 'responders' gem and Ember.js.

I have set up the ember.js, I have created a model to test my app ( Lead ) and the controller/layout/view that it needs. I use Rails 4.2, ruby 2.2 and MySQL.

So here is the controller I wrote in order to work with ember (api-style)

class Api::V1::LeadsController < ApplicationController
  respond_to :json

  def index
    respond_with Lead.all, layout: "application"
  end

  def show
    respond_with lead
  end

  def create
   respond_with :api, :v1, Lead.create( lead_params )
  end

  def update
    respond_with lead.update( lead_params )
  end

  def destroy
   respond_with lead.update( lead_params )
  end

private

  def lead
    Lead.find( params[:id] )
  end

  def lead_params
    params.require( :lead ).permit( :first_name, :last_name, :email, :phone, :status, :notes )
  end

end

I haven't changed the application_controller. I have the default layout (application.html.haml) and all the routing works fine. If I do not use the "respond_to :json" and the responders gem, everything works fine. As it is now, the layout is not rendered. I have to change it to "application.json.haml", but this is no good as I need the the markup of the layout and I want to yield inside it the "json" of my data.

Any ideas? So far I have not found a solution.

1

There are 1 best solutions below

2
On

It looks like you are trying to use the same endpoint to expose both the JSON API and serve your ember application. I would separate the two.