我正在使用FactoryGirl和Faker在我的seeds.rb文件中生成用户对象但由于某种原因,正在创建完全相同的用户并且rake db:seed由于电子邮件唯一性验证而失败.
用户工厂:
#users.rb
require 'faker'
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
phone Faker::PhoneNumber.cell_phone
email Faker::Internet.email
password "password"
password_confirmation "password"
end
end
Run Code Online (Sandbox Code Playgroud)
和seeds.rb文件中的代码:
#seeds.rb
rand(5..11).times { FactoryGirl.create(:user) }
Run Code Online (Sandbox Code Playgroud)
错误:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
如果我打开控制台并使用FactoryGirl.create(:user)我得到相同的结果......相反的对象正在反复创建,即使我只运行Faker::Internet.email几次我会收到几封电子邮件.
工厂女工:
[1] pry(main)> FactoryGirl.create(:user)
...
=> #<User id: 3, first_name: "Osvaldo", last_name: "Wunsch", email: "willy@damore.net", phone: "(912)530-4949 x64848", created_at: "2014-07-31 20:57:27", updated_at: "2014-07-31 20:57:27", encrypted_password: "$2a$10$mxWC7yLYR0m/Sw8MO6Lyru.xuTHCdCEuM9Orx3LXGApF...", reset_password_token: nil, reset_password_sent_at: …Run Code Online (Sandbox Code Playgroud) 我正在为Hartl的Rails 4 Tutorial的第6章练习.第一个练习测试以确保用户电子邮件地址正确缩小:
require 'spec_helper'
describe User do
.
.
.
describe "email address with mixed case" do
let(:mixed_case_email) { "Foo@ExAMPle.CoM" }
it "should be saved as all lower-case" do
@user.email = mixed_case_email
@user.save
expect(@user.reload.email).to eq mixed_case_email.downcase
end
end
.
.
.
end
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这里需要'重载'方法.一旦@user.email被设定为内容mixed_case_email并保存,不@user.reload.email和@user.email是一回事吗?我把重装方法拿出去尝试它并且似乎没有改变测试的任何东西.
我在这里错过了什么?
我正在使用Vagrant + VirtualBox为我的Rails应用程序设置虚拟机.我正在努力清理像这样.sh引用的配置脚本Vagrantfile:
config.vm.provision "shell", path: "script/provision-script.sh"
Run Code Online (Sandbox Code Playgroud)
配置脚本做了很多事情,但最后它应该安装rbenv Ruby版本,然后使用rbenv来安装Ruby 2.2.1.配置脚本的那一部分如下所示:
echo "setting up rbenv"
# execute the remaining commands as vagrant user, instead of root
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv.git ~vagrant/.rbenv"
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/ruby-build.git ~vagrant/.rbenv/plugins/ruby-build"
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~vagrant/.rbenv/plugins/rbenv-gem-rehash"
echo "setting up rbenv environment in bash"
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~vagrant/.bashrc
echo 'eval "$(rbenv init -)"' >> ~vagrant/.bashrc
# start …Run Code Online (Sandbox Code Playgroud) 我在一个Rails 4应用程序中使用Simple Form作为@user对象并拥有该行...
= f.input :entity_name
这会name='user[entity_name]'在input标记内生成HTML .我想改变它,因为我在控制器中做了一些自定义处理,但还没有找到办法.
我已经尝试将线路更改为......
= f.input :entity_name, name: 'entity[name]'
Run Code Online (Sandbox Code Playgroud)
...但这似乎根本不会影响生成的HTML.到目前为止,我还没有在Google/Stack Overflow上找到任何有此问题的人.
有没有人知道是否/如何通过Simple Form帮助器更改name属性?
提前致谢!
我正在尝试部署到Heroku但不能,因为默认的sqlite3服务器仍然存在.
检测到Heroku不支持的sqlite3 gem. https://devcenter.heroku.com/articles/sqlite3
在另一个使用Rails 3.2.13的教程中,我能够使用sqlite3作为dev db和Postgres作为生产数据库.Gemfile在Rails 4中看起来有所不同,但我修改它以使其具有:
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
Run Code Online (Sandbox Code Playgroud)
然后我更改了我的database.yml文件,以便生产部分看起来像这样:
production:
adapter: postgresql
database: my_production_database
pool: 5
timeout: 5000
Run Code Online (Sandbox Code Playgroud)
然后我跑bundle install和rake db:create和rake db:migrate,但我仍然无法推到的Heroku.于是,我就rake db:drop和rake db:create和rake db:migrate,但我仍然得到同样的错误消息.
我错过了什么吗?我还需要做些什么来确保我将Postgres作为我的生产数据库并且能够使用Heroku?
我下载并从这个链接安装的Git 1.8.4.2:http://git-scm.com/downloads.但是,当我git --version从终端运行时,我得到:
[~/workspace/ruby]: git --version
git version 1.7.4.4
Run Code Online (Sandbox Code Playgroud)
我试过重启终端和我的电脑.然后我尝试使用https://code.google.com/p/git-osx-installer/wiki/Uninstall上的信息.我在/ usr内的终端中执行了以下操作:
sudo rm -rf /usr/local/git
sudo rm /etc/paths.d/git
sudo rm /etc/manpaths.d/git
Run Code Online (Sandbox Code Playgroud)
然后我再次运行新的git .dmg文件,但是当我运行时仍然得到1.7.4.4 git --version.我想这里有一些我不明白的Mac文件系统.任何帮助将不胜感激!
我有以下ActiveAdmin表单:
form do |f|
f.inputs "Timesheet Details" do
f.input :jobs_assigned_worker, :label => "Worker", as: :select, collection: Worker.all
f.input :worked_time_hours, :label => "Worked Time (Hours)"
f.input :worked_time_mins, :label => "Worked Time (Minutes)"
f.input :driving_time_hours, :label => "Driving Time (Hours)"
f.input :driving_time_mins, :label => "Driving Time (Minutes)"
f.input :spent_dollars, :label => "Extra Money Spent"
end
f.actions
end
Run Code Online (Sandbox Code Playgroud)
当我在编辑视图中使用此表单时,选择下拉列表会自动默认为当前值.但是在生产中,下拉列表由于某种原因默认为顶部的空白值(为什么那里的空白值呢?).
编辑
问题似乎是ActiveAdmin不理解关联,并且默认情况下无法选择关联对象.我需要弄清楚如何f.input为关联编码.表格适用于时间表.时间表has_manyJobsAssignedWorkers和每个JobsAssignedWorker都有一个Worker.
我正在尝试学习如何使用Rspec的共享示例功能,并在运行测试时收到警告:
WARNING: Shared example group 'required attributes' has been previously defined at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
...and you are now defining it at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
The new definition will overwrite the original one.
....
Run Code Online (Sandbox Code Playgroud)
我已经阅读了我认为这里有关于这个问题的文档,但是我无法理解它/看到我案例的内容.
这是我的共享示例:
# spec/support/shared_examples/required_attributes_spec.rb
shared_examples_for 'required attributes' do |arr|
arr.each do |meth|
it "is invalid without #{meth}" do
subject.send("#{meth}=", nil)
subject.valid?
expect(subject.errors[meth]).to eq(["can't be blank"])
end
end
end
Run Code Online (Sandbox Code Playgroud)
我试图在User模型和Company模型中使用它.这是它的样子:
# spec/models/user_spec.rb
require 'rails_helper'
describe User do
subject { build(:user) }
include_examples 'required attributes', [:name] …Run Code Online (Sandbox Code Playgroud) 我正在开发一个遗留应用程序,最近升级到 Rails 6.0.4。该应用程序使用sprockets, NOT webpacker。
现在我正试图离开turbolinks和进入turbo-rails。此应用程序中的 js 文件均使用 sprockets require 指令,例如:
//= require jquery3
//= require turbolinks
Run Code Online (Sandbox Code Playgroud)
我似乎不知道如何将其应用到turbo-rails. 我已经安装了 gem 并删除了 Turbolinks,但我找到的所有文档都有类似import "@hotwired/turbo-rails".
我对前端的东西不是很了解,但我相信这是一个不同的(ES6?)语法。该应用程序目前已启用sprockets 3.7.2,我认为 sprockets 在版本 4 之前不支持 ES6。如果可能的话,我希望避免此升级触及更多依赖项...
是否可以//= require像我现在那样进行涡轮增压//= require turbolinks?
在我的应用程序中,我有几个Builder类,负责从外部API请求获取数据并构建/保存资源到数据库.我正在处理大量数据并实现了Parallel gem,以通过使用多个进程来加快速度.
但是,我发现使用Parallel的方法的任何测试都会失败并出现相同的错误:
ActiveRecord::StatementInvalid:
PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Run Code Online (Sandbox Code Playgroud)
以下是正在测试的代码示例:
class AirportBuilder < Resource
def build_from_collection
Parallel.each(object_producer, in_processes: 24) do |params|
instance = Airport.find_or_initialize_by(fsid: params[:fs])
build!(instance, params)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我已经对此进行了一些搜索,但谷歌的所有结果都与使用多个线程/进程有关,使测试套件运行得更快,这是一个不同的问题.
有关如何在不引起PG错误的情况下有效测试此问题的任何想法?我意识到我可能需要将某些东西排除在外但不太确定要存根什么但仍然有一个有意义的测试.
提前感谢任何可能提供帮助的人!
ruby ×6
forms ×2
rspec ×2
activeadmin ×1
database ×1
factory-bot ×1
faker ×1
git ×1
heroku ×1
html ×1
macos ×1
methods ×1
postgresql ×1
process ×1
rubygems ×1
seeding ×1
simple-form ×1
sqlite ×1
terminal ×1
testing ×1
turbolinks ×1
ubuntu ×1
updates ×1
vagrant ×1
virtualbox ×1
warnings ×1
web ×1