sinatra

How do I access Sinatra params using a symbol?

这一生的挚爱 提交于 2019-12-06 22:40:18
问题 In Sinatra, I use params to get the key/value passed through the URL query string. I noticed I can use either a string or a symbol as the key to get the value. So if the URL is: http://localhost:4567/x?a=1&b=2 Then: params[:a] # => "1" params["a"] # => "1" params.to_s # => '{"name"=>"x", "a"=>"1", "b"=>"2"}' params.class # => Hash I can tell params is a Hash. But this doesn't seem to be a common behavior of a Hash. h = {"a" => "1", "b" => "2"} h["a"] # => "1" h[:a] # => nil Can someone please

How do I get params attributes in post?

痴心易碎 提交于 2019-12-06 17:29:13
问题 I am using Sinatra with Ruby 1.8.7. I'm new to web development, so I don't totally understand get and post, but I got some stuff working. What I need to know next is how to interrogate params in post for certain attributes. In my main file, I have this code: get "/plan_design" do erb :plan_design end post "/plan_design" do # do stuff with params end In plan_design.erb, I have: <% if (hash[paramTitle].kind_of?(String)) %> <div> <input class="planDesignAsset" name="<%= paramTitle %>" value="<%=

will_paginate helper undefined in sinatra with mongoid

空扰寡人 提交于 2019-12-06 16:48:25
I am using sinatra 1.4.3 and mongoid 3.1.4. I tried adding will_paginate gem from master branch for mongoid support so I added this to my gemfile: gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => 'master' In environment.rb I added: require 'will_paginate' require 'will_paginate/mongoid' And pagination method started working. I have still problem with will_paginate helper. In my views I get errors like: NoMethodError: undefined method `will_paginate' for #<Class:0x006ff5df8578b0> Am I missing something for helper to work under sinatra? I don't know if it's

what is attr_accessor in datamapper - ruby

丶灬走出姿态 提交于 2019-12-06 15:46:05
I am a newby in datamapper. I saw this code in this forum. class User include DataMapper::Resource property :id, Serial property :email, String, :required => true, :unique => true, :format => :email_address, property :name, String property :hashed_password, String property :salt, String property :created_at, DateTime attr_accessor :password, :password_confirmation property would mean that it defines the field in the database table..what does attr_accessor means..is it kind of field in the model but not in the database.. thanks Yes, you are right. It is an attribute (a field) of your model, but

Errors with shotgun gem and msvcrt-ruby18.dll when running my Sinatra app

↘锁芯ラ 提交于 2019-12-06 15:41:04
Greetings, Every time I make a change to a Sinatra app I'm working on and try to refresh the browser (located at http://localhost:4567/ ) the browser will refresh and, the console window seems to restart the WEB brick server. The problem is that the content in the browser window does not change. A friend of mine told me it was a shotgun issue and referred me to rtomayko's shotgun gem: http://github.com/rtomayko/shotgun On this page I read that the shotgun gem would basically solve my problem, allowing the changes made to my app to show up in the browser window after I refresh it. So I

how can I use like query in ruby with sinatra?

爱⌒轻易说出口 提交于 2019-12-06 15:06:41
Here's the query I tried: @blogs = DB[:blogs].where(:title => params[:s_txt]).reverse_order(:id) In this query, I'd like to find blogs in my database. I also need to create a query that gives users more results. How can I do this? I also had this problem and only found this solution @blogs = DB[:blogs].where("title LIKE '%#{params[:s_txt]}%'").reverse_order(:id) Curious if there are better ways.. Searching in two fields is repeating the LIKE and seperating by AND or OR @blogs = DB[:blogs].where("title LIKE '%#{params[:s_txt]}%' or comment LIKE '%#{params[:c_txt]}%'").reverse_order(:id) or do

Using Sinatra and jQuery without redirecting on POST

a 夏天 提交于 2019-12-06 13:55:59
问题 I am trying to use jQuery to submit a form to my Sinatra app, but when POSTing via the AJAX, the Sinatra app is displaying a blank page. I would like it to stay on the same page, and update the content I have specified in the javascript. Here is my code, stripped down: post '/register' do register( params ) end get '/register' do haml :register end And here is my javascript in the haml file: :javascript $(function() { $("button#submit").click(function(){ $.ajax({ type: "POST", url: "/register

rspec clean database before running

狂风中的少年 提交于 2019-12-06 12:23:07
问题 I'm using RSpec in a Sinatra project. I'm testing a model which runs an after_find hook to initialize some values. I need to wipe the database before every run, and after some searching around, my spec helper file looks like this: require "pry" require "factory_girl_rails" require "rspec-rails" FactoryGirl.find_definitions ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.use_transactional_fixtures = true end I'm getting the

Sinatra doesn't show exceptions in log file

天大地大妈咪最大 提交于 2019-12-06 11:52:53
I'm coming from Rails to sinatra, and I have some problems using logging. I have a Sinatra app, that does it's logging like so: configure do Logger.class_eval { alias :write :'<<' } logger = Logger.new("log/#{settings.environment}.log") use Rack::CommonLogger, logger end All requests are logged properly, I see sth like 127.0.0.1 - - [25/May/2013 10:34:21] "GET / HTTP/1.1" 200 30 0.0021 127.0.0.1 - - [25/May/2013 10:34:22] "GET /favicon.ico HTTP/1.1" 404 18 0.0041 Inside the log files. But I also want to log application errors to the logfile. When I start my app in production env with RACK_ENV

Sinatra: three logs

大憨熊 提交于 2019-12-06 11:47:14
I'm using a very simple Sinatra app that works well. However, every log message is repeated three times. I can bring that down to two by disabling the Sinatra logging with disable :logging but I still have two. The messages are slightly different, so I gather they are coming from Rack and somewhere else in the stack too. How do I completely disable logging of successful web requests? Rack is adding own logging as a middleware try to run rackup -E none This removes one log entry. The second one is sinatra native which you've already disable. And the third one is Rack::Lint logging if I remember