ruby on rails - How can I make RABL render an attribute only if it exists in the object? -


i have restful user model , trying filter attributes included in show json response when user object being sought not current user.

logic:

if params[:id]==current_user   json => {:name: <name value>, :birthdate: <birthdate>} else   json => {:name: <name value>} end 

when filter attributes in controller, missingattribuiteerror following code:

userscontroller

def show   if params[:id]==current_user.id.to_s     @user=user.find(params[:id])   else     @user=user.find(params[:id], :select => [:name])   end   respond_with(@user) end 

users/show.json.rabl

object @user attributes :name, :birthdate 

how can make rabl render attribute if exists in object?

apparently easy things should easy. works.

[not sure if filtering on view level better or worse filtering in controller, though.]

userscontroller

def show     @user=user.find(params[:id])     respond_with(@user) end 

users/show.json.rabl

if @user==current_user   attributes :birthdate end 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -