sinatra

Run Ruby file from html form submit

柔情痞子 提交于 2020-01-06 06:54:15
问题 I have a Ruby program that reads a file and returns a certain output. I have to now create a web app of this program using Sinatra. I created a form with all the file options and I want to now run that Ruby code with that selected file from the form after the submit button is pressed. Basically, I’m not sure how to get this external Ruby program to run with the the filename that was selected by the user from the HTML form . The Ruby program ( example.rb ) starts with the definition def read

URL Map an entire namespace using rack mount?

梦想的初衷 提交于 2020-01-06 03:57:27
问题 I have two modular Sinatra rack based applications: core.rb & project.rb : # core.rb class Core < Sinatra::Base get "/" do "Hello, world!" end end # project.rb class Project < Sinatra::Base get "/" do "A snazzy little Sinatra project I wish to showcase." end get "/foo" do "If you see this, congratulations." end end My goal is simply to map the entire /projects namespace to the Project class, wheras everything else is handled by the Core class. I found that you can do this to a limited extent

Sinatra Net::HTTP causes timeouts on a simple request

此生再无相见时 提交于 2020-01-05 04:22:18
问题 I have a small simple Net::HTTP POST request to do to my Sinatra app: def collect(website) uri = URI("http://localhost:9393/save/#{website}") res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50') puts res.body end But it causes a timeout. Here is the request handler: post '/save/:website' do |website| puts request.body "done" end I never reach the puts nor the done . My shotgun server is running on port 9393 of course. When I use the REST Console extension and paste valid json in it,

RDiscount :generate_toc with Sinatra

强颜欢笑 提交于 2020-01-04 09:11:11
问题 So I have sinatra setup with Rdiscount to render a markdown file with a HAML layout. This all works but I want RDiscount to generate a table of contents based on the headers in my haml file. I've tried setting it in the sinatra configuration. set :markdown, :generate_toc => true but that doesn't seem to work. I've also tried doing it when I render the markdown like so: markdown :PAGENAMEHERE, :layout => :'layouts/PAGENAMEHERE', :generate_toc => true which also doesn't work. Is this even

How do you remove a route from Sinatra?

对着背影说爱祢 提交于 2020-01-04 03:14:04
问题 I have some dynamically loaded plugins which register their routes when they start up, however I also need to be able to remove their routes when they are disabled. Is there a way to remove existing routes? The API didn't have any methods I could find to remove them, and the only other way I could think to do it would be to go right to the @routes object in Sinatra::Base, but I am not sure if you can do anything to that, and if you can... is it safe to do? 回答1: Looking through the code for a

Sinatra tests always 404'ing

不想你离开。 提交于 2020-01-04 02:54:34
问题 I have a very simple Sinatra app which I'm having trouble testing. Basically, every single request test returns a 404 when I know from testing in the browser that the request works fine. Any ideas as to what the problem might be? test_helper.rb: ENV["RACK_ENV"] = 'test' $: << File.expand_path(File.dirname(__FILE__) + '/../lib') require 'app' Sinatra::Synchrony.patch_tests! class Test::Unit::TestCase include Rack::Test::Methods end app_test.rb require 'test_helper' class AppTest < Test::Unit:

Sinatra server won't start - “wrong number of arguments”

ⅰ亾dé卋堺 提交于 2020-01-03 19:34:48
问题 I wanted to try Sinatra as I've heard it's better for the newbie web-dev than rails.. and in general I prefer more minimalistic than not. To premise this, I'm using ruby 2 and whatever version of sinatra that gem install gets. All I've done so far is the basic require 'sinatra' get '/' do 'Hello, World!' end try to run the server with ruby basics.rb and it throws this at me: /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/thin-2.0.0.pre/lib/thin/server.rb:108:in `initialize': wrong number of

How do I reuse connections in ActiveRecord?

陌路散爱 提交于 2020-01-03 12:23:09
问题 I was playing around with Sinatra and ActiveRecord when I found that a connections are not reused automatically by this simple API. #!/usr/bin/env ruby require 'sinatra' require 'active_record' ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'newsletter.db' ) ActiveRecord::Schema.define do create_table :subscribers do |t| t.string :email t.timestamps end end class Subscriber < ActiveRecord::Base validates :email, presence: true end class Newsletter < Sinatra::Base set

Exclude some paths from global authentication in Sinatra

我与影子孤独终老i 提交于 2020-01-03 01:35:08
问题 I have an API in Sinatra, using a middleware doing a global restrict authentication with token. This middleware inserts the authentication check in a before statement, in order to protect everything globally without the need to add a check in each route definition. before do denied unless authorized? or env['PATH_INFO'] == '/auth/login' or env['REQUEST_METHOD'] == 'OPTIONS' # For AngularJS headers checks end But now I have some routes I need to exclude from this global restrictions (only 2 or

AngularJS POSTs empty requests?

假装没事ソ 提交于 2020-01-02 15:41:06
问题 I'm a newbie in AngularJS and I've faced an issue when I try to make a POST request with AngularJS and it POSTs no parameters with it. I use Sinatra as a RESTful interface. That's how my Sinatra backend looks: post '/layer/:layer_id' do @layer = PageLayer.where(id: params[:layer_id]).first @layer.content = params[:content] @layer.save end If try to POST with Postman chrome extension - it works! Sinatra saves the content properly. So I'm sure that the backend works as it should. That's how my