0

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.

manosagent
  • 614
  • 8
  • 18
  • The data is rendered in json format, as expected. The problem is that the layout is not rendered at all, unless I change it to "application.json.haml", but if I do that I cannot use any markup in order to add the div that is needed for Ember to be "initialized". – manosagent Feb 12 '15 at 11:57

1 Answers1

0

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.

Oren Hizkiya
  • 4,420
  • 2
  • 23
  • 33
  • I don't quite get it. Could you be a little more specific? – manosagent Feb 12 '15 at 12:27
  • You said "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." - I think you are trying to serve the layout for your application along with the JSON in it. What should happen instead is that you serve the layout of your application and the application itself makes a call to load the JSON. – Oren Hizkiya Feb 12 '15 at 12:35