Ruby / Sinatra – serving up css, javascript, or image files

Sinatra and Rails use the path public for static content – e.g., ./public/javascripts/. All files in these paths would then be served by the web server (e.g. Thin, Passenger), but without the need for /public in the URL (e.g. the file at #{my_app_root}/public/javascripts/application.js would be available via the Web at the URL http://#{my_domain}/javascripts/application.js).

How to do a Post/Redirect/Get using Sinatra?

Redirect in Sinatra is the most simple to use. So the code below can explain: require ‘rubygems’ require ‘sinatra’ get “https://stackoverflow.com/” do redirect “http://example.com” end You can also redirect to another path in your current application like this, though this sample will delete a method. delete ‘/delete_post’ do redirect ‘/list_posts’ end A very common place … Read more

What’s the best way to talk to a database while using Sinatra?

If you’re using Sinatra, I can’t recommend DataMapper highly enough. I have a couple Rails apps where I’m stuck with ActiveRecord, and I’m constantly cursing its shortcomings and design flaws. If you’re on Sinatra, DataMapper is a very practical choice. require “rubygems” require “sinatra” require “datamapper” DataMapper.setup(:default, “sqlite3::memory:”) class Post include DataMapper::Resource property :id, Integer, … Read more

How to use Pry with Sinatra?

Summary Use require ‘pry’ at the top of your application. Call binding.pry in your code whenever you want to drop into the interactive session. For information on using Pry, see Turning IRB on its head with Pry and the Pry wiki. When you are done with a particular interactive session, type exit or Ctrl-D; Sinatra … Read more

How do I get Sinatra to refrain from adding the X-Frame-Options header?

Sinatra uses Rack::Protection, in particular the frame_options option, which is what is setting the X-Frame-Options header. You can configure which protections are used. Sinatra turns most of them on by default, (some are only enabled if you also are using sessions, and Rack::Protection itself doesn’t enable some by default). To prevent sending the X-Frame-Options header … Read more

Sinatra + Bundler?

Inside your Sinatra app, you just have to require the bundler setup: require “bundler/setup” require “sinatra” get “https://stackoverflow.com/” do “Hello world!” end Alternatively, if you don’t want to add the additional require “bundler/setup” at the top of your app, you can instead invoke sinatra via bundle exec (e.g. bundle exec ruby myapp.rb) This assumes that … Read more

Any success with Sinatra working together with EventMachine WebSockets?

Did not try it, but should not be too hard: require ’em-websocket’ require ‘sinatra/base’ require ‘thin’ EM.run do class App < Sinatra::Base # Sinatra code here end EM::WebSocket.start(:host => ‘0.0.0.0’, :port => 3001) do # Websocket code here end # You could also use Rainbows! instead of Thin. # Any EM based Rack handler should … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)