我使用Faker gem来播种某些数据.我怎样才能设置最大值.假Company.name的长度,以及如何设置假数字的范围?
name = Faker::Company.name
Run Code Online (Sandbox Code Playgroud)
这里我想包括最大长度,因为name有最大的模型限制.40个字符.
code_id = Faker::Number.number
Run Code Online (Sandbox Code Playgroud)
对于code_id,我想要一个从1到50的范围.我试过,code_id = Faker::Number.number(from=1, to=50)但这似乎不正确,因为播种它产生了以下错误:
ArgumentError: wrong number of arguments (2 for 1)
/usr/local/rvm/gems/ruby-2.1.5/gems/faker-1.4.3/lib/faker/number.rb:4:in 'number'
Run Code Online (Sandbox Code Playgroud)
我应该如何调整Faker以满足我的需求?
我正在 Rails 中设计邮件程序布局。这是不同邮寄者将使用的一般布局。在总体布局的中间,我想包含一个链接,但前提是它user_mailer.rb与def account_activation(user)发送电子邮件的方法一起使用。有没有办法做到这一点?
在我尝试的布局中:
<% if mailer_name == 'user' && ['account_activation'].include?(action_name) %>
Run Code Online (Sandbox Code Playgroud)
但这产生了错误:
undefined local variable or method 'mailer_name' for #<#<Class:0x007f18d0701f30>:0x00000006109c70>
Run Code Online (Sandbox Code Playgroud)
如果我改变mailer_name,以mailer使其不再产生一个错误,但它并没有显示链接是(尽管我期待在预览的ACCOUNT_ACTIVATION方法。
我使用 Faker gem 为我的数据库植入虚假数据。对于某些变量,我例如使用Faker::Lorem.paragraph(4). 但我真正想要的是用带有样式的文本播种。比如<p></p><b>等等。这可以用 Faker 宝石吗?
鉴于:
class Author < ActiveRecord::Base
has_many :articles
end
Run Code Online (Sandbox Code Playgroud)
收到包含作者文章的JSON文件后,我想销毁作者数据库中的文章,但不包含在JSON文件中.如何为每个循环创建"?"?
article_collection = @author.articles.all
unless article_collection.blank?
article_collection.each do |article_from_collection|
# How can I check on the next line if it's id is included in the params?
if article_from_collection.arti_id "is not part of" params[:author][:articles_attributes]
article_from_collection.destroy
end
end
end
Run Code Online (Sandbox Code Playgroud)
我如何检查第5行,根据arti_id哪一项也包含在参数中,是否arti_id存在JSON输入中存在的文章?
我是否应该arti_id在参数中构建s 的集合,然后用于.include?查看数据库中的每篇文章,如果它在此集合中?
我尝试了以下两行.false无论文章是否包含在json中,第一个返回.第二行返回错误TypeError Exception: no implicit conversion of Symbol into Integer.
if params[:author][:articles_attributes].include? article_from_collection.arti_id
if params[:author][:articles_attributes][:arti_id].include? article_from_collection.arti_id
Run Code Online (Sandbox Code Playgroud)
params[:author] 返回类似于:
{ "some_other_attributes"=>[ {"key"=>"value", …Run Code Online (Sandbox Code Playgroud) 我不小心使用 mysql 命令行删除了一个表。幸运的是桌子是空的。
但我需要这张桌子回来。所以我只想重新运行该表的迁移,而不是其他表,因为这些表仍然存在。
当我运行时knex migrate:latest,它返回“已经是最新的”。如果我也一样npm run migrate name-of-specific-miration-file。
我用膝关节。如何仅运行我希望它运行的特定迁移文件?
在控制器方法内部,我有下面的代码块。Visual Studio Code 给出错误:
无法重新声明块范围变量“count”和“rows”
但我不明白这一点,因为这些变量是在不同的情况下声明的,并且只有 1 个情况会触发。我应该如何处理这个问题?
switch (tab) {
case "active":
const { count, rows } = await User.findAndCountAll({
where: {
...query,
},
offset,
limit,
});
break;
case "favourites":
const { count, rows } = await User.findAndCountAll({
where: {
...query,
[Op.or]: [{ name: {[Op.like]: `%${searchText}%` }}],
},
offset,
limit,
});
...
Run Code Online (Sandbox Code Playgroud) 我想根据用户所在网站的哪个页面显示不同的标题菜单.所以基本上在Rails' application.html.erb我想要的东西:
<% if "user is op page x" %>
<%= render 'layouts/header1' %>
<% else %>
<%= render 'layouts/header2' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但这条线<% if "user is op page x" %>需要什么呢?也就是说,是否有一个指定页面的Rails方法?
Update2:我已经清理了代码,这似乎解决了一些问题。我已将新代码作为新问题发布在此处。
更新:组织和用户有一对多的关系。我的问题涉及需要组织和用户的联合注册表单。在 maxcal 对原帖的帮助之后,我create为我的嵌套表单(“组织有很多用户”)编写了一个新方法,如下所示。我也添加begin...rescue...end到create方法中。现在的情况/问题:
有人知道代码有什么问题吗?问题似乎更多是嵌套用户而不是组织(父)。此外,users_attributes.empty?不起作用,因为根据日志,空提交的表单仍然包含此类属性:
Parameters: {"utf8"=>"?", "authenticity_token"=>"***", "organization"=>{"name"=>"", "bag"=>"", "users_attributes"=>{"0"=>{"email"=>"", "username"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "usertype"=>"2", "admin"=>"true"}}}, "commit"=>"Register"}
Run Code Online (Sandbox Code Playgroud)
.
def create
@organization = Organization.new(new_params.except(:users_attributes))
begin
if users_attributes.empty?
@organisation.errors.add(:users, 'No user provided')
end
@organization.transaction do
@organization.save!
if users_attributes.any?
@organization.users.create!(users_attributes)
end
end
rescue ActiveRecord::RecordInvalid => invalid
if @organization.persisted?
if @organization.users.any?
@organization.users.each do |single_user|
single_user.send_activation_email
end
end
flash[:success] = "Confirmation email sent."
redirect_to root_url
else
@organization.users.build if @organization.users.blank?
render …Run Code Online (Sandbox Code Playgroud) 我有以下控制器方法:
const org = await organisation.toJSON().name;
// The next line works fine, that's not the problem
const users = await organisation.getUsers(organisation.toJSON().id, User);
await Promise.all(users.forEach(async user => {
await sendAccountEmail(
user.email,
user.surname,
org
);
}));
Run Code Online (Sandbox Code Playgroud)
对于该Promise行,我收到一个错误:
UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“Symbol(Symbol.iterator)”
知道我的代码有什么问题吗?
ruby ×6
node.js ×3
faker ×1
javascript ×1
knex.js ×1
loops ×1
mysql ×1
nested-forms ×1
npm ×1
rubygems ×1
seeding ×1
sequelize.js ×1
validation ×1