Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rails helpers issue #341

Closed
prashantham opened this Issue · 4 comments

5 participants

@prashantham

I am having issue with rails helpers when templates are rendered directly

Rabl.render(comment, 'comments/show', :view_path => 'app/views', :format => :hash)

=> undefined method `number_to_human_size' for #Rabl::Engine:0x00000003843110

But no issues when sent through server.
Are the helpers not available when rendered directly?

@nesquena
Owner

You have to pass a 'scope' that has access to those helpers. If you are in a controller or view the its automatic because the 'scope' is by default self. But if not, you need to provide a scope that has access:

Rabl.render(comment, 'comments/show', :view_path => 'app/views', 
  :format => :hash, :scope => @has_access_to_helpers)
@databyte
Collaborator

Yep, if you just pass in your actual controller or helper into scope - it should work.

@vintikzzz

Hello! I have the simmilar issue. I have a code in my controller

@posts = Post.order_by([:created_at, :desc]).page(1)
posts = Rabl.render(nil, 'posts/index', :view_path => 'app/views', :format => :hash, :scope => self)

My view

object false
node(:current_page) { @posts.current_page }
node(:total_items) { @posts.total_count }
node(:per_page) { @posts.limit_value }
node(:total_pages) { (@posts.offset_value / @posts.limit_value) + 1 }
node :items do
  @posts.map do |p|
    partial 'posts/post', object: p, root: false
  end
end

As you can see i have a partial in my template. So when i call this page i got

Missing partial posts/post with {:locale=>[:ru, :en], :formats=>[nil], :handlers=>[:erb, :builder, :haml, :rabl]}. Searched in: * "/Users/vintik/Projects/backbone-twitter-bootstrap/app/views" * "/Users/vintik/.rvm/gems/ruby-1.9.3-p125/gems/twitter-bootstrap-rails-2.1.4/app/views" * "/Users/vintik/.rvm/gems/ruby-1.9.3-p125/gems/kaminari-0.14.1/app/views" * "/Users/vintik/.rvm/gems/ruby-1.9.3-p125/gems/devise-2.1.2/app/views" * "/Users/vintik/Projects/tbone-devise/app/views" * "/Users/vintik/Projects/tbone/app/views"

I tried another approach in my controller

@posts = Post.order_by([:created_at, :desc]).page(1)
posts = JSON.parse(render_to_string('posts/index.json.rabl'))

And it works. But it is really bad to convert to json and back.

@databyte databyte was assigned
@pauldatta

If you are using Rabl with Grape and are having trouble accessing view helpers within Rabl templates when an API call is made to Grape, use this - https://gist.github.com/4419597

Assumes that your Rabl templates are in a /views/api/ path - should make a difference though if you have set this as root.

@nesquena nesquena closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.