ccocchi / rabl-rails

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

Render with ActionController #28

Closed jyr closed 11 years ago

jyr commented 11 years ago

Hi

I tried improve performance app using "ActionController::Metal" in controller like http://newaperio.com/blog/19-fast-json-api-controllers-in-rails but using rabl-rails. I don't know as add the rabl's render

any tips?

Sorry, for posting here.

ccocchi commented 11 years ago

rabl-rails registers itself to ActionView so you should be able to use it when you use ActionView to render your response. I'd use at least ActionController::Rendering and ActionController::MimesRespond in your controller to use the render keyword. You may have to specify the :format and :handler options when using render

jyr commented 11 years ago

@ccocchi I have

class ApiPropertiesController < ActionController::Metal
      include ActionController::Rendering
      include ActionController::Renderers::All
      include ActionController::ImplicitRender
      include ActionController::MimeResponds

      respond_to :json

     def agent
         respond_with(@property = Property.joins(:agent).find(params[:api_property_id]))
     end
end

I get

[(135)
    {
        "agent_id": 556,
        "nty_id": null,
        "office_id": 145,
        "property_type_id": 2,
        "title": "4984-RAV DEPTO EN VTA CORPUS CRISTY (Tamaulipas)",
        "updated_at": "2013-03-30T00:10:32Z"
     }
]

do not use my agent.json.rabl template. I want to get

[(135)
    {
        "agent": {id: 556, name: "Luis"},
        "nty_id": null,
        "office_id": {id: 14, name: nil},
        "property_type_id": {id: 2, name: "Casa"},
        "title": "4984-RAV DEPTO EN VTA CORPUS CRISTY (Tamaulipas)",
        "updated_at": "2013-03-30T00:10:32Z"
     }
]
ccocchi commented 11 years ago

Remove the ActionController::Renderers::All and you will why: no default views path has been set. It must be set by another module you didn't include. You can add append_view_path 'app/views' manually and it will use your template.

jyr commented 11 years ago

ok, works with

class ApiPropertiesController < ActionController::Metal
  include ActionController::Rendering
  include ActionController::ImplicitRender
  include ActionController::MimeResponds

  append_view_path 'app/views'

  respond_to :json

 def agent
     respond_with(@property = Property.joins(:agent).find(params[:api_property_id]))
 end
end

In my template I have "node(:url) { |property| property_url(property) }" but I get

undefined method `property_url'

I need add another helper of rabl in my controller?