我希望git停止在我们的存储库中跟踪我的本地开发日志(log/development.log).这可能吗?我该怎么办?
如何*args
在Rails中有条件地向数组添加哈希?如果存在原始值,我不想踩踏原始值.
例如,我有一个接收数组的方法:
def foo(*args)
# I want to insert {style: 'bar'} into args but only if !style.present?
bar(*args) # do some other stuff
end
Run Code Online (Sandbox Code Playgroud)
我已经开始使用rails提供的extract_options和reverse_merge方法:
def foo(*args)
options = args.extract_options! # remove the option hashes
options.reverse_merge! {style: 'bar'} # modify them
args << options # put them back into the array
bar(*args) # do some other stuff
end
Run Code Online (Sandbox Code Playgroud)
它有效,但看起来很冗长,而且不是很红宝石.我觉得我错过了什么.
将现有项模型添加到新的付款模型时,会出现错误"无法找到ID = 123的项目,ID = 123".这是一个has_many关系并使用accepts_nested_attributes_for.
class Payment < ActiveRecord::Base
has_many :items
accepts_nested_attributes_for :items
...
class Item < ActiveRecord::Base
belongs_to :payment
...
Run Code Online (Sandbox Code Playgroud)
付款和项目模型是假设的,但问题是真实的.在保存付款(在创建操作中完成)之前,我需要将项目与付款关联(就像我在新操作中一样).用户在创建付款时需要修改项目的属性(添加帐单代码就是一个例子).
具体来说,错误发生在控制器的创建操作中:
# payments_controller.rb
def new
@payment = Payment.new
@payment.items = Item.available # where(payment_id: nil)
end
def create
@payment = Payment.new payment_params # <-- error happens here
...
end
def payment_params
params.require(:payment).permit( ..., [items_attributes: [:id, :billcode]])
end
Run Code Online (Sandbox Code Playgroud)
lib/active_record/nested_attributes.rb:543:in 'raise_nested_attributes_record_not_found!'
紧接在callstack之前.很奇怪ActiveRecord包含payment_id作为搜索条件的一部分.
为了完整,表单看起来像这样......
form_for @payment do |f|
# payment fields ...
fields_for :items do |i|
# item fields
Run Code Online (Sandbox Code Playgroud)
并在新操作上正确呈现项目.通过表单的params看起来像这样:
{ "utf8"=>"?", …
Run Code Online (Sandbox Code Playgroud) 在FactoryGirl的最新版本中,一些句法方法如有Factory.create
利于其他几种语法折旧,最显着FactoryGirl.create
且更简单create
.
但是,经验表明,鉴于上下文,某些语法并不总是合适的.
举个例子:
FactoryGirl.define do
factory :article do
after_create {|a| a.comments << create(:comment) }
end
factory :comment do
end
end
Run Code Online (Sandbox Code Playgroud)
文章has_many评论,评论属于文章.在上面的工厂中,a.comments << create(:comment)
发出错误Comment(#nnn) expected, got FactoryGirl::Declaration::Static
.将该行更改为a.comments << FactoryGirl.create(:comment)
,错误消失.
目前尚不清楚何时一种语法优先于任何其他形式.
使用Rails,rspec和capybara,我正在尝试测试典型的ERB生成形式:
<form action="/pages/1" id="edit_page_1" method="post">
<input id="page_title" name="page[title]" type="text">
<input name="commit" type="submit" value="Update Page">
</form>
Run Code Online (Sandbox Code Playgroud)
我运行两种功能规格,无论语言是什么,以及I18N特定的功能(用于国际化测试).
问题是没有明确的方法来点击与capybara的提交按钮,除非我错过了明显的.我希望click('commit')
能够做到这一点.
click_button('Update Page')
作品但显然是特定于语言的,即使它们呈现相同的表单模板,也不能与"新建"和"编辑"模板一起使用.我的 Rails 4.1.5 应用程序在 Ruby 2.1 上,并且使用 postgresql 和 hstore for store_accessor。从一个新的数据库开始:
rake db:migrate; rake db:seed
Run Code Online (Sandbox Code Playgroud)
...一切正常。
再次回滚和前进,这就是问题开始的地方:
rake db:migrate VERSION=0; rake db:migrate; rake db:seed
Run Code Online (Sandbox Code Playgroud)
如果我尝试访问它的任何“属性”,模型会报告“未定义的方法访问器”。
undefined method `accessor' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0xyaddayadda>
Run Code Online (Sandbox Code Playgroud)
该模型将 store_accessor 与 postrgesql 的 hstore 结合使用。模型定义如下:
class Foo < ActiveRecord::Base
...
store_accessor :properties, :color, :material, :brand
...
end
Run Code Online (Sandbox Code Playgroud)
迁移定义如下:
class CreateFoos < ActiveRecord::Migration
def change
enable_extension "hstore"
create_table :foos do |t|
t.hstore :properties
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
为了使问题消失,我可以在重新创建数据库之前完全炸掉它:
rake db:migrate VERSION=0; rake db:drop:all; rake db:create; rake db:migrate; rake …
Run Code Online (Sandbox Code Playgroud) 我需要从字符串中删除/剪切前300个单词或字符.
这意味着,从一开始我就需要字符串中有限数量的字符.
截断的东西.
有这个功能吗?
activerecord ×2
ruby ×2
arduino ×1
capybara ×1
factory-bot ×1
git ×1
hstore ×1
postgresql ×1
rspec ×1