Created
March 17, 2016 20:05
-
-
Save bootcoder/b38abaf166cf6d9cdbd3 to your computer and use it in GitHub Desktop.
Return multiple sets within json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/json' | |
get "/posts" do | |
@posts = Post.order("created_at DESC") | |
erb :'posts/index' | |
end | |
post "/posts" do | |
p params | |
@post = Post.new(params[:post]) | |
if @post.save | |
if request.xhr? | |
# | |
# erb :'/posts/_post', layout: false, locals: { post: @post } | |
page = erb :'/posts/show', layout: false, locals: { post: @post } | |
json post: @post, page: page | |
else | |
redirect "posts/#{@post.id}" | |
end | |
else | |
# this is where errors should be handled | |
end | |
end | |
get "/posts/new" do | |
if request.xhr? | |
erb :'/posts/_form', layout: false | |
else | |
erb :'posts/new' | |
end | |
end | |
get "/posts/:id" do | |
@post = Post.find(params[:id]) | |
erb :'posts/show' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment