如何在Ruby on Rails中使用mongoid检查文档是否存在嵌入文档?假设我有一个user包含name,email可能有nicknames嵌入文档的文档.现在,如果我运行user.first.nicknames,如果user没有nicknames嵌入文档,它将会出错.我试过了matches?,exists?但他们没有工作.
谢谢!
目前,我有,当我同时使用返回相同值的会话变量session["test"]或session[:test].这两个是一样的吗?使用一个比另一个更好吗?
谢谢
我查看了gem wiki并按照说明进行操作,但由于某种原因,我nil在进行omniauth测试时得到了一个:
user_sessions_controller_spec.rb:
require 'spec_helper'
describe UserSessionsController, "OmniAuth" do
before do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end
it "sets a session variable to the OmniAuth auth hash" do
request.env["omniauth.auth"]['uid'].should == '123545'
end
end
Run Code Online (Sandbox Code Playgroud)
spec_helper.rb:
RACK_ENV = ENV['ENVIRONMENT'] ||= 'test'
OmniAuth.config.test_mode = true
omniauth_hash =
{:provider => "facebook",
:uid => "1234",
:info => {:name => "John Doe",
:email => "johndoe@email.com"},
:credentials => {:token => "testtoken234tsdf"}}
OmniAuth.config.add_mock(:facebook, omniauth_hash)
Run Code Online (Sandbox Code Playgroud)
规格结果:
Failures:
1) UserSessionsController OmniAuth sets a session variable to the OmniAuth auth hash …Run Code Online (Sandbox Code Playgroud) 目前,我的本地开发环境设置为能够发送实际的电子邮件.因此,正在进行交付,ActionMailer::Base.deliveries表中没有任何内容.是否可以在黄瓜测试中禁用电子邮件发送?如果是这样,这样做的语法是什么?或者有更好的方法来测试发送的电子邮件吗?
authentication_steps.rb:
Then /^I should receive a confirmation email$/ do
email = ActionMailer::Base.deliveries.last
email.subject.should == "Welcome to our website!"
end
Run Code Online (Sandbox Code Playgroud)
应用程序/配置/ development.rb
...
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
# Setup for local testing of emails using gmail test account
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => 'smtp.sendgrid.net',
:port => 587,
:domain => ENV['MAIL_DOMAIN'],
:authentication => 'plain',
:enable_starttls_auto => true,
:user_name => ENV['MAIL_USERNAME'],
:password => ENV['MAIL_PASSWORD']
}
...
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在开发一个具有以下黄瓜步骤的项目:
Given /^no registered users$/ do
User.delete_all
end
Run Code Online (Sandbox Code Playgroud)
作为一个新的RoR用户,即使我在我们的开发数据库上进行测试,这看起来有点危险,因为我们的User表有实际数据.什么是代码行?
谢谢!
目前在我的开发环境中,使用后我没有看到ActionMailer::Base.deliveries表中的任何内容mail.我缺少一个设置吗?
config/environments/development.rb :
config.action_mailer.delivery_method = :test.
应用程序/邮寄者/ notifier.rb
def send_email(user)
mail(:to => user.email, :subject => "Welcome to our website!")
end
Run Code Online (Sandbox Code Playgroud)
这些是我正在运行的测试:
When /^I signup$/ do
visit signup_path
@user = FactoryGirl.build(:user)
fill_in "user_email", :with => @user.email
fill_in "user_password", :with => @user.password
click_button "Join now"
end
Then /^I should receive a confirmation email$/ do
email = ActionMailer::Base.deliveries.last
email.subject.should == "Welcome to our website!"
end
Run Code Online (Sandbox Code Playgroud)
现在我得到以下错误I should receive a confirmation email:
undefined method subject for nil:NilClass …
我目前正在使用以下内容来获取表中某个字段的数组:
Classrooms.all.map(&:teacher_name)
Run Code Online (Sandbox Code Playgroud)
这将返回以下内容:
["James", "Josh", "Peter"]
Run Code Online (Sandbox Code Playgroud)
我想要的是一个散列而不是像下面这样的东西,我可以在其中包含教师 ID:
{"James" => "1", "Josh" => "2", "Peter" => "3"}
Run Code Online (Sandbox Code Playgroud)
我尝试使用,Classrooms.all.map(&:teacher_name, &:teacher_id)但它给了我一个语法错误。
谢谢!
目前,以下内容允许我为#container div下的所有id附加一个处理程序,其中包含"test":
$('#container').delegate("input[id*='test']", 'click', function(event) {
// code
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做同样的事情,但寻找所有没有测试的id?
谢谢!
现在在我的代码中,我试图检查特定的会话键/值是否存在,如果存在,则执行某些操作。有没有办法一次完成所有这些而不是多个 if 语句?
这是我的 if 语句:
if session[:mysession] %>
if session[:mysession][:cats] != "fluffy" %>
#do something
end
end
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到一个零错误:
if session[:mysession][:cats] == "fluffy"
#do something
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
我目前正在使用以下代码来检查我的页面是否有特定的文本:
it { should have_selector('test_container', text: 'hiya') }
Run Code Online (Sandbox Code Playgroud)
我所检查的只是页面上的一个跨度:
<span id="test_container">hiya</span>
Run Code Online (Sandbox Code Playgroud)
如何测试这样的特定跨度标签的内容?我不想检查整个页面的文本“hiya”,而是检查如上所述的特定跨度标签。
谢谢!
我已经在代码中注意到我正在send使用的经常使用的代码我并不完全理解它.使用它而不是不使事情变得复杂而不使用它有什么好处?
一个例子:
def star_ratings_count(rating)
self.send("#{rating}_ratings".to_sym).star.count
end
def update_star_ratings_count
self.ratings_count = star_ratings_count(:criteria_one_rating)
end
Run Code Online (Sandbox Code Playgroud) 目前,当存在验证错误时,将在我的表单中呈现divwith class="field_with_errors":
<form method="post" action="/users" accept-charset="UTF-8">
<div class="field_with_errors">
<input id="user_email" type="text" value="tom@test.com" name="user[email]">
</div>
<input id="user_password" type="password" value="mypass" size="30" name="user[password]">
<input id="user_submit" type="submit" value="Submit" name="commit">
</form>
Run Code Online (Sandbox Code Playgroud)
如果不使用额外的gem并在Rails 3环境中,有没有办法修改验证错误的呈现方式?例如,我希望在带有错误div的input标记之后呈现验证错误:
<input id="user_email" type="text" value="tom@test.com" name="user[email]"><div class="field_with_errors"></div>
Run Code Online (Sandbox Code Playgroud)
另外,有没有办法呈现特定的错误?如果是电子邮件错误,则div在输入文本框之后呈现并生成"重新输入您的电子邮件"消息.
(到目前为止,我所做的大多数搜索都使用gems作为解决方案.)
我想B_array基于现有的阵列A 创建一个新阵列A_array.如果该项目A_array具有某个字段,则将其添加到其中B_array.
目前这就是我所拥有的,它将所有内容放入B_array:
B_array = A_array.map {|item| if item.name == 'Josh'}
Run Code Online (Sandbox Code Playgroud)
A_array:
[id:0,name:"Josh",email:"josh@josh@gmail.com"],
[id:1,name:"Scott",email:"scott@josh@gmail.com"],
[id:2,name:"Josh",email:"dan@josh@gmail.com"]
Run Code Online (Sandbox Code Playgroud)
期望的输出B_array:
[id:0,name:"Josh",email:"josh@josh@gmail.com"],
[id:2,name:"Josh",email:"dan@josh@gmail.com"]
Run Code Online (Sandbox Code Playgroud)
谢谢!
ruby-on-rails ×12
ruby ×3
rspec ×2
capybara ×1
cucumber ×1
javascript ×1
jquery ×1
mongoid ×1
omniauth ×1
oop ×1
validation ×1