Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active April 11, 2019 03:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save a2ikm/5072882 to your computer and use it in GitHub Desktop.
Save a2ikm/5072882 to your computer and use it in GitHub Desktop.
Use Jbuilder within Sinatra.
# coding: utf-8
require "bundler"
Bundler.require
helpers do
def current_user
@current_user ||=
Hashie::Mash.new(admin?: [true, false].sample)
end
def edit_article_url(article)
"http://example.com/articles/#{article.id}/edit"
end
def author_url(author)
"http://example.com/authors/#{author.id}"
end
end
get '/' do
@article = Hashie::Mash.new(id: 1, name: "DRAGONBUSTER02", published_at:"2012-01-10")
@article.author = Hashie::Mash.new(id: 1, name: "Mizuhito Akiyama")
@article.comments = Array.new(1) { |i| Hashie::Mash.new(id: i+1, name: "@ikm", content: "My missing book...") }
jbuilder :index
end
source "https://rubygems.org"
gem "sinatra"
gem "tilt-jbuilder", ">= 0.4.0", :require => "sinatra/jbuilder"
gem "hashie"
# Place this file in the `views` directory.
json.(@article, :id, :name, :published_at)
json.edit_url edit_article_url(@article) if current_user.admin?
json.author do |json|
json.(@article.author, :id, :name)
json.url author_url(@article.author)
end
json.comments @article.comments do |json, comment|
json.(comment , :id, :name, :content)
end
@a2ikm
Copy link
Author

a2ikm commented Mar 3, 2013

@a2ikm
Copy link
Author

a2ikm commented Mar 26, 2013

Now tilt-jbuilder 0.4.0 supports Sinatra.

@ggoral
Copy link

ggoral commented Dec 11, 2013

This example is just what I needed! Thank you!

@tomwang1013
Copy link

I use jruby + sinatra and did what as you said above. But got an error:
ArgumentError - wrong number of arguments calling json (0 for 1):
/home/wangxiantong/.rvm/gems/jruby-1.7.4@sinatra/gems/tilt-jbuilder-0.5.3/lib/tilt/jbuilder.rb:51:in evaluate' org/jruby/RubyKernel.java:1093:ineval'
/home/wangxiantong/.rvm/gems/jruby-1.7.4@sinatra/gems/tilt-jbuilder-0.5.3/lib/tilt/jbuilder.rb:51:in evaluate' org/jruby/RubyKernel.java:1093:ineval'

@a2ikm
Copy link
Author

a2ikm commented Dec 1, 2014

@tomwang1013
Sorry for my late reply.
tilt-jbuilder's implementation to evaluate jbuilder expressions seems changed since v0.6.0.
So, if you're still getting same problem, you should try with its latest version (maybe v0.6.1).

@ziggurat
Copy link

This is great!! Is there a way of configuring the views path? I have a more pod like folder structure and would like to be able to use the view from the same folder as my route file definition.

Thanks!!!

@fakefarm
Copy link

Make sure you're requiring sinatra/jbuilder. Then, in views/index.jbuilder things work as expected.
In my example below, I'm skipping options with a {} and passing in local data

require "sinatra"
require 'sinatra/jbuilder'

get '/movies/:id' do
  jbuilder(:index, {}, flick: Flick.movie(params[:id]))
end

I learned this from the tilt docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment