我有一个非常简单的Sinatra应用程序,我在测试时遇到了问题.
基本上,当我从浏览器中的测试中知道请求正常工作时,每个请求测试都会返回404.关于问题可能是什么的任何想法?
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
Run Code Online (Sandbox Code Playgroud)
app_test.rb
require 'test_helper'
class AppTest < Test::Unit::TestCase
def app
@app ||= Sinatra::Application
end
def test_it_says_hello
get "/"
assert_equal 200, last_response.status
end
end
Run Code Online (Sandbox Code Playgroud)
app.rb
$: << 'config'
require "rubygems" require "bundler"
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
require ENV["RACK_ENV"]
class App < Sinatra::Base register Sinatra::Synchrony
get '/' do
status 200
'hello, I\'m bat shit crazy and ready to rock'
end
end
Run Code Online (Sandbox Code Playgroud)
的Gemfile
source …Run Code Online (Sandbox Code Playgroud) 我正在构建一个Sinatra应用程序,该应用程序需要将文件发送给刚从其他位置下载的用户(实际上是代理)。
尽管我可以完成大部分工作,但我无法让Sinatra发送代码中指定的正确文件名。我使用的代码是:
get '/' do
attachment file.name
headers['Content-Type'] = file.content_type
headers['Content-Disposition'] = 'attachment'
file.body
end
Run Code Online (Sandbox Code Playgroud)
(file.body是文件的内容)
如何让Sinatra使用所需的文件名?
为什么以下代码导致Foo :: People:Class'的错误'undefined local variable或method`foo_client'
class Foo::People
class << self
def get_account_balance(account_num)
foo_client.request :get_account_balance, :body => {"AccountNum" => account_num}
end
end
def foo_client
@@client ||= Savon::Client.new do|wsdl, http|
wsdl.document = PEOPLE_SERVICE_ENDPOINT[:uri] + "?WSDL"
wsdl.endpoint = PEOPLE_SERVICE_ENDPOINT[:uri]
end
end
end
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
class Foo
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name
validates :name, uniqueness: true
end
Run Code Online (Sandbox Code Playgroud)
但是,在测试我收到的唯一性验证时:
/Users/neil/.rvm/gems/ruby-1.9.3-p286@config-keeper/gems/activemodel-3.2.9/lib/active_model/validations/validates.rb:96:in `rescue in block in validates': Unknown validator: 'UniquenessValidator' (ArgumentError)
from /Users/neil/.rvm/gems/ruby-1.9.3-p286@config-keeper/gems/activemodel-3.2.9/lib/active_model/validations/validates.rb:93:in `block in validates'
from /Users/neil/.rvm/gems/ruby-1.9.3-p286@config-keeper/gems/activemodel-3.2.9/lib/active_model/validations/validates.rb:90:in `each'
from /Users/neil/.rvm/gems/ruby-1.9.3-p286@config-keeper/gems/activemodel-3.2.9/lib/active_model/validations/validates.rb:90:in `validates'
from /Users/neil/code/open_source/config_keeper/app/models/foo.rb:8:in `<class:App>'
Run Code Online (Sandbox Code Playgroud)
这看起来有点奇怪.关于什么可能是错的任何想法?
这是routes.rb:
map.resources :assignments, :shallow => true do |assignment|
assignment.resources :problems
end
Run Code Online (Sandbox Code Playgroud)
如何在代码中获取编辑问题的URL(/ assignments/xyz/problems/abc/edit)?我已经尝试了
edit_assignment_problem_path(赋值,问题)
和edit_problem_path(问题).
虽然第一个适用于我的本地设置,但在服务器上它表示没有定义edit_assignment_problem_path方法.有任何想法吗?
我有以下型号:
class Person < ActiveRecord::Base
has_many :accounts, :through => :account_holders
has_many :account_holders
end
class AccountHolder < ActiveRecord::Base
belongs_to :account
belongs_to :people
end
class Account < ActiveRecord::Base
has_many :people, :through => :account_holders
has_many :account_holders
end
Run Code Online (Sandbox Code Playgroud)
但是,在使用这种关系时我遇到了问题.Account.first.account_holders工作正常,但Account.first.people返回:
NameError: uninitialized constant Account::People
from /Users/neil/workspace/xx/vendor/rails/activesupport/lib/active_support/dependencies.rb:105:in `const_missing'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/base.rb:2204:in `compute_type'
from /Users/neil/workspace/xx/vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/base.rb:2200:in `compute_type'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `send'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `klass'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:73:in `find_target'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:353:in `load_target'
from /Users/neil/workspace/xx/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:139:in `inspect'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
如果没有找到记录,有没有办法做一个引发异常的find_by_x?
我有一个用户模型,检查一个值是否已更改before_save(通过运行User.zipcode_changed?).这个想法是,如果有的话,这将排除延迟的工作.
问题是,当我从头开始迁移应用程序时,我收到一个错误:
An error has occurred, all later migrations canceled:
undefined method `postcode_changed?' for #<User:0x105db6158>
Run Code Online (Sandbox Code Playgroud)
因此,我应该把它们放在哪里?模特是错的地方吗?
我的应用程序控制器中有以下内容:
before_filter :set_current_subdomain
protected
def set_current_subdomain
Thread.current[:current_subdomain] = current_subdomain
@account = Account.find_by_subdomain(current_subdomain)
end
def current_subdomain
request.subdomain
end
Run Code Online (Sandbox Code Playgroud)
然后我的一些模型中的以下内容:
default_scope :conditions => { :account_id => (Thread.current[:account].id unless Thread.current[:account].nil?) }
Run Code Online (Sandbox Code Playgroud)
现在,这有效 - 有些时候.我举例说明了加载索引方法并获取应用了作用域的记录列表,但有时候得到一个空列表,因为Thread.current [:account_id]的结果为nil,即使请求中较早的查询正在运行使用相同的值.
问题是,为什么这不起作用,是否有更好的方法来设置当前请求的全局变量?
我正在尝试构建一个打印流程,其中包括使用Rails打印一批财务应用程序.
我正在打印大约100个应用程序,其中包含多个级别的数据(应用程序本身,子模型及其子模型).
目前该页面的效率非常低,因为它正在进行大量的N + 1查询,这导致性能很差.
问题是,是否有一种从数据库中获取此数据的有效方法.我已经尝试includes()为所有子模型提取表单,但这对于低于该模型的模型没有帮助(例如,income_line_items在financial_history模型上)
有任何想法吗?
我正在寻找一个持续集成服务器:
那里有适合的解决方案吗?我看过Hudson和Teamcity,但想知道那里还有什么.