her icon indicating copy to clipboard operation
her copied to clipboard

Use FirstLevelParseJSON, but the return data always contains metadata, response_errors, attributes and etc.

Open chandler-huang opened this issue 10 years ago • 1 comments

Hi sir,

When i use the latest version of her on my rails project, i follow the document to set up and use her. Now i have two questions about the return value from her.

Background: OS: Mac gem 'rails', '4.1.5' gem 'rails-api'

Example: I have an Affiliate model and initialize her like:

Her::API.setup url: 'http://localhost:3000' do |c|
  # Request
  c.use Faraday::Request::UrlEncoded

  # Response
  c.use Her::Middleware::DefaultParseJSON

  # Loger

  # Adapter
  c.use Faraday::Adapter::NetHttp
end

And in the controller, the code is

affiliate = Affiliate.find(9991104)
render json: affiliate

And get a result like this:

{
metadata: { },
response_errors: { },
destroyed: false,
attributes: {
name: "1",
affiliate_code: "1",
affiliate_url: "1",
description: "1",
is_deleted: true,
created_date: "2013-12-17T07:34:00.000+00:00",
created_by: "shuang",
modified_date: "2015-11-06T02:50:27.567+00:00",
id: 9991104
},
changed_attributes: { }
}

So how can I get the response only include attributes like

{
name: "1",
affiliate_code: "1",
affiliate_url: "1",
description: "1",
is_deleted: true,
created_date: "2013-12-17T07:34:00.000+00:00",
created_by: "shuang",
modified_date: "2015-11-06T02:50:27.567+00:00",
id: 9991104
}

And another question is when i call Affiliate.all in controller

affiliates = Affiliate.all
render json: affiliates

And the result is

{
message: "stack level too deep"
}

Dose anyone encounter the same problem like me? Thanks for all of you help.

chandler-huang avatar Dec 19 '15 15:12 chandler-huang

# config/initializers/her.rb
Her::Model.class_eval do
  def as_json(options = nil)
  attributes.as_json(options)
  end
end
# controllers - no change here in theory
affiliate = Affiliate.find(9991104)
render json: affiliate

iMacTia avatar Jun 19 '17 11:06 iMacTia