我有一个包含10个属性的表单.
其中我有4个属性,我需要应用我称之为"互为条件存在"的Active Record验证.这些属性是
这意味着如果用户填写其中一个,那么所有其他人必须在场
到目前为止,我只能说,如果用户填写第一个属性"地址行1",那么所有其他属性必须存在.
但它并未验证所有可能组合中的所有MUTUAL存在.例如,如果用户让"地址行1"为空但填充了zipcode并将其他三个留空,我希望活动的不再验证表单,因为他应该被要求填写其他三个属性.对每个属性都是如此.
这该怎么做?
这是我目前的代码
规格/型号/用户
validates :address_line_1,
presence: true,
length: { maximum: 100,
minimum: 3 }
validates :zipcode,
presence: true, if: :address_line_1?,
length: { maximum: 20,
minimum: 4}
validates :state,
presence: true, if: :address_line_1?,
validates :country,
presence: true, :address_line_1?,
length: { maximum: 50}
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord
在我的Rails 4应用程序中,我对Postgres 9.4数据库进行了此查询:
@chosen_opportunity = Opportunity.find_by_sql(
" UPDATE \"opportunities\" s
SET opportunity_available = false
FROM (
SELECT \"opportunities\".*
FROM \"opportunities\"
WHERE ( deal_id = #{@deal.id}
AND opportunity_available = true
AND pg_try_advisory_xact_lock(id) )
LIMIT 1
FOR UPDATE
) sub
WHERE s.id = sub.id
RETURNING sub.prize_id, sub.id"
)
Run Code Online (Sandbox Code Playgroud)
非常受到dba.SE相关答案的启发.
我只是希望我的查询找到并更新第一个(随机,有LIMIT)行,available = true并将其更新到available = false,我需要在执行此操作时锁定行,但不要让新请求等待释放上一个锁,因为有许多将使用此查询的并发调用.
但我也看到了NOWAIT选项FOR UPDATE.我不确定我理解使用pg_try_advisory_xact_lock()和NOWAIT选项之间的区别,他们似乎在我看来实现了同样的目标:
要防止操作等待其他事务提交,请使用该
NOWAIT选项.
在我的 ruby on Rails 4.2 应用程序中,在一个页面上,我有一个条件后端规则,该规则在类的更改中转换为页面前端
<div id="content">
<i class='<% if x=true %>glyphicon glyphicon-thumbs-down<% else> glyphicon glyphicon-thumbs-up ><% end %>' </i>
this is the message.
</div>
Run Code Online (Sandbox Code Playgroud)
如何使用 rspec 3/capybara 检查页面是否包含类 glyphicon-thumbs-down 或类 glyphicon-thumbs-up ?
我尝试了下面的代码,但失败了:
it "should sheck one of the 2 classes presence" do
expect(page).to have_css '.glyphicon-thumbs-down' || expect(page).to have_css '.glyphicon-thumbs-up'
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
syntax error, unexpected tIDENTIFIER, expecting keyword_end (SyntaxError)
Run Code Online (Sandbox Code Playgroud) 我正在建立一个每日交易Rails应用程序来学习RoR.
我在过去的几个小时内遇到了一个问题:我无法获得活动管理员上其他相关模型的模型属性.让我告诉你确切的问题:
我有两种模式:品牌(即交易的品牌)和交易.交易属于品牌,但品牌可以有很多交易.
models/deal.rb是这样的:
class Deal < ActiveRecord::Base
belongs_to :brand
Run Code Online (Sandbox Code Playgroud)
我们有models/brand.rb:
class Brand < ActiveRecord::Base
has_many :deals
attr_accessible :name
Run Code Online (Sandbox Code Playgroud)
我在我的迁移中做了t.belongs_to,所以这没关系.
在Active Admin的交易'创建表单中,我输入与交易相关联的品牌作为管理员:
管理员/ game.rb
ActiveAdmin.register Deal do
# -- Form -----------------------------------------------------------
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :brand_id, :label => "Select a brand:", :as => :select, :collection => Brand.all
end
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我可以创建特定品牌的优惠.但我无法在我的交易列表中显示品牌的名称:
ActiveAdmin.register Deal do
index do
selectable_column
# id_column
column :title
column :deal_amount
column :brand do |deal|
link_to deal.brand.name
end
Run Code Online (Sandbox Code Playgroud)
......不起作用.
我怎样才能做到这一点 ?
我尝试了一切,但我基本上不知道如何获取品牌的名称,因为它与Deal的表中的brand_id相匹配 …
我是Ruby on Rails的新手,我不能根据它的数量改变一个单词的结尾.
在下面的代码顶部附近,你会看到我写的
h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}
Run Code Online (Sandbox Code Playgroud)
这里的"u"取一个名为PRIZE_TYPE的数组中的值之一.我怎么能这样做
如果count = 1,则显示"该交易有1个累积奖金"
如果count = 4,则显示"该交易有4个累积奖金
注意:计数不能是= 0或<0
这是我的代码:
panel "Details of Prizes" do
PRIZE_TYPES.each do |u|
table_for deal.prizes.where(:prize_type => u) do |t|
h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}s
initially set in the campaign for a total value of #{custom_number_to_currency (deal.category_prizes_total_initial_value(u)) }", class: 'title-within-table'
if deal.prizes.where(:prize_type => u).count > 0 # if there is at least one record of 'jackpot prize'
t.column("Prize") { |prize| link_to( …Run Code Online (Sandbox Code Playgroud) 我在第一个div上有一个图像背景.然后我想要一个div(这里为了方便橙色背景显示)和另一个带有注释的"上面"框(称为"注释框").
问题是我不希望"橙色区域"的内容显示在它之下.
以下是我目前的情况:
我当前代码的问题是我在"橙色区域"div中放置的一些内容会在评论框下面,这很难看.
这是一个jsfiddle:http://jsfiddle.net/uu0xe2xL/1/:
HTML
<body class='example-page'>
<div id="content">
<div class="page-main-image" >
</div>
<!--div with orange background -->
<div id="orange-zone">
</div>
<!-- comment box -->
<div class="comment-box comment" style="opacity: 1;">
xxx it's cool to comment<br/>
nice one dude
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
CSS
html {
height:100%;
}
body.example-page {
height:100%;
}
#content {
height: 100%;
margin-top: 18px;
@media (max-width: 767px) {
margin-top: 9px;
}
}
.page-main-image {
height: 100%;
background: url('http://www.thedrum.com/uploads/drum_basic_article/115509/main_images/BBCb.jpg'); background-size: cover; background-repeat: no-repeat;
} …Run Code Online (Sandbox Code Playgroud) 我希望看到此列,我可以在这里查看https://developers.google.com/web/fundamentals/performance/resource-prioritization
但是我在Chrome Dev工具的“网络”标签中看不到它。我的Chrome浏览器的版本是63.0,我认为是最近的版本。
我需要在我的Rails应用程序中为测试设置不同的日期/时间,即:
我使用了http://danilenko.org/2012/7/6/rails_timezones/来获取Time.zone.today但我无法在任何地方找到如何在2天后在CERTAIN时间说
尝试过滤Active Admin上的资源时遇到问题.
当我将它们应用于标准内容时,过滤器实际上可以正常工作,但这里是多选的.
确实在我的交易模型中:有一个"Deal_goal"属性,我可以选择许多Deal_goal,例如"获取新用户"或"生成热门话题"
以下是我在活动管理表单中输入的方式:
f.input :deal_goal,
:label => "Deal goals",
:as => :select,
:multiple => true,
:collection => DEAL_GOALS
Run Code Online (Sandbox Code Playgroud)
要进行多重选择,我在Deal模型中对其进行序列化
应用程序/模型/ deal.rb
class Deal < ActiveRecord::Base
serialize :deal_goal, Array
Run Code Online (Sandbox Code Playgroud)
我也使用staal.io建议多选('选择'宝石)
我没有做的是在我的索引页面(交易列表)上,对我的Deal_goal属性进行过滤,如果可能的话,在这里有一个多选项,使我能够根据多种目标过滤交易(对于示例:过滤处理目标#1或目标#3)
我尝试了admin/deal.rb下面的两个,但没有一个工作!
filter :deal_goal, :as => :select, :collection => DEAL_GOALS
filter :deal_goal, :as => :multiple_select, :collection => DEAL_GOALS
Run Code Online (Sandbox Code Playgroud)
有人有线索吗?
我刚刚使用Spork和Guard一起使用 Zeus
我使用了这个一步一步的指南:http://blog.blenderbox.com/2014/04/10/testing-rails-3-with-guard-and-zeus/
事情,现在是我的惯例
在终端窗口做$ zeus开始
在另一个终端窗口:守卫
在另一个窗口:$ rspec
我的测试工作正常,但我很惊讶rspec测试套件比Spork慢,因为大多数人都认为这是测试速度的巨大提升.
还有什么让我觉得有一个错误就是当我输入rspec时,它会在运行测试之前显示一条消息,读取下面的内容:
No DRb server is running. Running in local process instead
Run Code Online (Sandbox Code Playgroud)
有人知道这是什么问题?
谢谢
guardfile
require 'active_support/core_ext'
require 'active_support/inflector'
# NEw ZEUS guard
# source - blog.blenderbox.com/2014/04/10/testing-rails-3-with-guard-and-zeus/
guard 'zeus-client', :all_on_start => false, :all_after_pass => false do
ignore %r{^\.zeus\.sock$}
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", …Run Code Online (Sandbox Code Playgroud) ruby ×5
activeadmin ×3
rspec ×3
css ×2
html ×2
activerecord ×1
capybara ×1
concurrency ×1
css3 ×1
guard ×1
javascript ×1
postgresql ×1
rspec2 ×1
sql ×1
zeus ×1