我已将我的一个应用程序从Rails 4.2.6升级到Rails 5.0.0.该升级指南说,是自动加载功能在生产中默认情况下禁用现在.
现在我总是在生产服务器上出错,因为我在application.rb文件中加载了所有带有自动加载的lib 文件.
module MyApp
class Application < Rails::Application
config.autoload_paths += %W( lib/ )
end
end
Run Code Online (Sandbox Code Playgroud)
现在,我已经设定了config.enable_dependency_loading,true但我想知道是否有更好的解决方案.必须有一个原因,默认情况下在生产中禁用自动加载.
我正在尝试将我的sqlite3数据库迁移到postgresql,但我无法通过此错误.
当我跑 taps server sqlite://db/development.sqlite3 [user] [password]
我一直在 /Users/phillipjarrar/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:298:in <module:Templates> : uninitialized constant Tilt::CompileSite (NameError)
我有多个带有电子邮件验证的模型。因此,我已将验证提取到自定义验证器中。我按照Rails Guides的教程进行了修改。
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || "is not an email")
end
end
end
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。但是由于我已经将电子邮件验证的功能提取到了它自己的范围内,所以我也想单独测试它。我不想为每个模型添加相同的电子邮件格式测试。
我发现了另一个问题,它也问了同样的问题,但对于 RSpec。但是由于我还没有使用过存根和模拟,我不知道如何将测试移植到 Minitest 测试中。我还没有找到任何使用 Minitest 测试自定义验证器的资源。
有谁知道如何在 Minitest 中为自定义验证器编写此类测试(不使用规范!)?
validation unit-testing ruby-on-rails custom-validators minitest
我有一个RESTful HTTP API。现在,我想将HTTPS请求从我的S7 PLC发送到API。我已经联系了西门子,他们说他们没有HTTPS客户端实现。
但是tcpClient库中有一个模块。而且由于TCP是HTTP over SSL的基础,所以我想问问是否有人已经实现了HTTPSClient或有人知道从哪里开始?
该API仅可通过HTTPS访问以保护隐私。
我有一个User存储用户电话号码的自定义类。
class User {
let phoneNumber: String
}
Run Code Online (Sandbox Code Playgroud)
如何从用户通讯录中获取对应的联系人?
我尝试了以下方法,但似乎这仅适用于联系人姓名,因为我总是得到nil:
let predicate = CNContact.predicateForContactsMatchingName(userInstance.phoneNumber)
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactPhoneNumbersKey]
// Is already permitted
try! CNContactStore().unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch).first // This returns nil
Run Code Online (Sandbox Code Playgroud)
我在文档中进行了搜索,但没有找到合适的解决方案。
ActiveRecord::InvalidForeignKey in ArticlesController#destroy
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "articles" WHERE "articles"."id" = ?
Run Code Online (Sandbox Code Playgroud)
我正在创建一个博客应用程序,每当我尝试删除其中包含注释的文章时,我都会收到此错误.我该如何解决?
让我知道要发布的代码,我将更新问题.
文章管理员:
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def index
#@articles = Article.all
@articles = Article.paginate(:page => params[:page], :per_page => 10)
end
def show
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article …Run Code Online (Sandbox Code Playgroud) ruby ×2
activerecord ×1
autoload ×1
cncontact ×1
https ×1
ios ×1
macos ×1
minitest ×1
plc ×1
rubygems ×1
sinatra ×1
swift ×1
taps ×1
unit-testing ×1
validation ×1