killebytes
killebytes

Reputation: 980

as_json with options not working in views

Does as_json() or to_json() in views works with options?

<%- @profiles.each { |p| %>
<%= p.as_json(:only => [:name])  %>
<%- } %>

I can never get this to work, it always return the whole object as json.

Upvotes: 0

Views: 116

Answers (2)

Ajite
Ajite

Reputation: 86

I am also using Rails 3.2.9, I have copied and pasted your code into my user view and it worked just fine.

Try to create a new application with a generated profiles scaffold to see if it works properly.

Upvotes: 1

shweta
shweta

Reputation: 8169

if <%= p.as_json(:only => [:name]) %> returns {"profile"=>{"name"=>"profile_name"}}, You can replace with:

<%- @profiles.each { |p| %>
  <%= p.as_json(:only => [:name])["profile"]["name"]  %>
<%- } %>

Upvotes: 0

Related Questions