Rails – Display object and a collection in RABL template -


i'm using rabl foundation json api. need display both object (@user) , collection (@posts) in template. typically, no problem. @posts child of @user, so, this:

object @user     attributes :id, :name    child :posts |post|... 

the problem is, run @posts through helper method (by_category) in controller so:

@user = user.find(params[:id]) @posts = @user.posts.by_category(params[:category]) 

by fetching posts child node, don't apply helper method – , can't fetch posts by_category.

i'm wondering how best approach this, , if there's way display both object , collection in same rabl template?

right the documentation, can include locals in partials example:

you can pass in other instance variables used in template as:

rabl::renderer.new('posts/show', @post, :locals => { :custom_title => "hello world!" }) 

then, in template, can use @custom_title as:

attribute :content node(:title) { @custom_title } 

this allows use like:

controller:

rabl::renderer.new('user/show', @user, :locals => { :posts_category => "examples" }) 

partial:

attribute id node(:posts) { |user| user.posts.by_category(@posts_category) }  

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -