Note that there are some explanatory texts on larger screens.

plurals
  1. PODraper: How to convert all allowed attributes and public methods of the decorated model into json
    primarykey
    data
    text
    <p>I'm looking for an easy way to convert decorated model into json to use in my client-side templates. I'd like to find a solution where all allowed attributes and public methods of the decorated model will persist in json.</p> <p>Currently I have backbone RIA with rails backend. I'm using haml_coffee_assets gem for client-side templating. Draper is there to provide decoration for my User model</p> <pre><code>class ContactDecorator &lt; Draper::Base include Draper::LazyHelpers decorates :user allows :login, :id, :total_unread, :total_messages def for_json { :new_messages =&gt; new_messages, :avatar_link =&gt; avatar_link, :login_name_link =&gt; login_name_link, :id =&gt; model.id, :name =&gt; model.login, :avatar =&gt; model.avatar.url(:small), :humanized_messages_number =&gt; } end def humanized_messages_number pluralize(user['total_messages'], t("share_my_trip.messages.Message"), t("share_my_trip.messages.Messages")) + " #{new_messages}" end def new_messages model['total_unread'].to_i &gt; 0 ? "(#{model['total_unread'].to_i} #{t("share_my_trip.messages.new")})" : '' end def avatar_link link_to(image_tag(model.avatar.url(:small), :size =&gt; "32x32", :onerror =&gt; "this.src='/avatars/original/missing.png'"), share_my_trip_user_path(:id =&gt; model.login), :id =&gt; "user-nick-#{model.id}", :class =&gt; "author") end def login_name_link link_to(model.login, share_my_trip_user_path(:id =&gt; model.login), :id =&gt; "user-nick-#{model.id}", :class =&gt; "author") end end </code></pre> <p>my controller code looks like:</p> <pre><code>def index @dialogs = ContactDecorator.decorate(current_user.contacts).collect{|c| c.for_json} end </code></pre> <p>and than in my view I'm just initializing backbone app:</p> <pre><code>:javascript App.init({dialogs: #{@dialogs.to_json}}) </code></pre> <p>Is there a way to delete ugly #for_json decorator method?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload