我有一个我正在尝试测试的课程,其中包括ActiveModel::Validations
module SomeModule
class SomeClass
include ActiveModel::Validations
end
end
Run Code Online (Sandbox Code Playgroud)
我试图在不spec_helper保持快速的情况下测试它,但require 'activemodel'规范顶部的简单方法不起作用。我不断收到一个uninitialized constant SomeModule::SomeClass::ActiveModel(NameError). 对于规格文件:
require 'activemodel'
describe SomeModule::SomeClass do
end
Run Code Online (Sandbox Code Playgroud)
有什么解决这个问题的建议吗?提前致谢!
我想从我的自定义验证消息中删除该属性并只显示该消息,而不是
School Please Provide Your School Name
Run Code Online (Sandbox Code Playgroud)
我想回来
Please Provide Your School Name
Run Code Online (Sandbox Code Playgroud)
在我的模型中设置
validates :school, presence: { message: 'Please Provide Your School Name' }
Run Code Online (Sandbox Code Playgroud)
消息作为 JSON 响应返回。
查看 full_messages 方法
# File activemodel/lib/active_model/errors.rb, line 348
def full_messages
map { |attribute, message| full_message(attribute, message) }
end
Run Code Online (Sandbox Code Playgroud)
我可以用
# File activemodel/lib/active_model/errors.rb, line 348
def full_messages
map { |attribute, message| full_message(message) }
end
Run Code Online (Sandbox Code Playgroud)
我试过这个
module ActiveModel
class Errors
def full_messages
map { |attribute, message| full_message(message) }
end
end
end
Run Code Online (Sandbox Code Playgroud)
位于 /lib/active_model/errors.rb
但是当我尝试运行测试(rspec)时出现错误 …
我有一个ActiveModel类,其实例仅在被触摸时才有效。以下代码有效:
class Base
include ActiveModel::Model
validates :touched?, inclusion: { in: [true] }
def update(params = {})
initialize(params)
@touched = true
end
def touched?
!!@touched
end
end
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它。这不太好。我想写这样的东西:
validates: touched?, equality: true
Run Code Online (Sandbox Code Playgroud)
有没有更好/更短的编写方式而不使用自定义验证器?如果它也适用于booleans以外的值,那就太好了。
Rails 指南提供的 I18n 范围特定于ActiveModel::Validations内部ActiveRecord对象的使用。例如:
en:\n activerecord:\n errors:\n models:\n some_model:\n attributes:\n name:\n blank: "Please enter your full legal name."\nRun Code Online (Sandbox Code Playgroud)\n\nActiveModel::Validations以这种方式使用时,这将不起作用:
class SomeModel\n include ActiveModel::Validations\n validates :name, presence: true\nend\nRun Code Online (Sandbox Code Playgroud)\n\n相反,使用框架默认值“\xe2\x80\x9ccan\t be Blank”。
\n\n如何解决这个问题?
\n尝试将 Rails 6.0.0 与 Mongoid 6.1.0 一起使用
gem 'rails', '~> 6.0.0' gem 'mongoid', '~> 6.1.0'
已经尝试不在 Gemfile 中设置 mongoid 版本,但仍然无法正常工作。
Bundler 找不到 gem "activemodel" 的兼容版本:
在 Gemfile 中:
mongoid (~> 6.1.0) 被解析为 6.1.1,这取决于 activemodel (~> 5.0
rails (~> 6.0.0) 被解析为 6.0.0,这取决于 activemodel (= 6.0.0)
我需要一些帮助构建一个表,然后从Rails 3中获取该表中的数据.
这是分解:
模型 - 这里涉及的3个模型是:
活动表:
id | thread_id | participants
Run Code Online (Sandbox Code Playgroud)
示例记录看起来像:
1 | 300 | 3,1,5,67,13
2 | 333 | 3,12
3 | 433 | 1,12
4 | 553 | 1,12, 67
Run Code Online (Sandbox Code Playgroud)
如果参与者是user_ids列表,如果有更好的方法来存储user_ids,请告诉我.我还没有建造它.
填充活动表后.然后,我希望能够按以下方式进行查询:选择参与者字段中包含67的participant_id的所有活动记录.
我希望以上内容清楚,如果不是,请告诉我.想法?思考?建议.
谢谢
我们正在使用Mustache模板,我想在我们的RoR Web应用程序中创建一个预览View,它将模板和我们存储在数据库中的一些数据结合起来,但它不能像我预期的那样工作,并且在对互联网进行一些搜索之后(包括所以!),我没有找到包含活动模型的任何示例.
如何将ActiveModel记录传递给Mustache以与模板合并?
设置:
架构
create_table "templates", :force => true do |t|
t.string "kind"
t.text "data"
t.integer "data_count"
end
create_table "bars", :force => true do |t|
t.string "guid"
t.string "name"
t.string "summary"
end
Run Code Online (Sandbox Code Playgroud)
这些模型没有什么特别之处.两者都是ActiveRecord :: Base的子类
class Bars < ActiveRecord::Base
end
class Templates < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
控制器
class TemplateController < ApplicationController
def preview
@result = Mustache.render( template.data, :bars => Bar.limit(template.data_count ) ).html_safe
end
end
Run Code Online (Sandbox Code Playgroud)
风景
<%= @result %>
Run Code Online (Sandbox Code Playgroud)
路线
get 'templates/:id/preview' => 'templates#preview', :as => 'templates_preview'
Run Code Online (Sandbox Code Playgroud)
数据
y …Run Code Online (Sandbox Code Playgroud) Rails 3.2.1
应用名称:demo
数据库:mongoDB与mongoid
我在rails 3.2.1中设置了这个脚手架:localhost:3000/pages,
我有这些领域:标题,内容.
我已经为每个字段填充了3行数据.
问题是:删除应用程序(删除根文件夹)并使用相同的脚手架(页面)创建相同的应用程序名称(演示)后,这3行数据仍然显示,我不再需要了.
任何人都可以如何清理数据库?谢谢
在Rails中为模型设置默认值的正确方法是什么?
class User < ActiveRecord::Base
attr_accessible :name, :points
end
Run Code Online (Sandbox Code Playgroud)
我希望积分从0开始而不是nil.理想情况下,默认值是立即创建的,而不是等待User保存到数据库中.但我想使用before_save或数据库约束也可以工作:
class User < ActiveRecord::Base
attr_accessible :name, :points
before_save :set_defaults
private
def set_defaults
self.points = 0
end
end
Run Code Online (Sandbox Code Playgroud)
使用最新的稳定Rails.
activerecord ruby-on-rails activemodel ruby-on-rails-3 ruby-on-rails-3.2
我正在尝试使用shoulda匹配器进行一些基本的rspec测试,并且我遇到了一个错误,我之前没有看过这个问题.
我有一个名为name的唯一属性,但由于项目所需的原因,我在config/locales/en.yml中覆盖了我自己的消息形式的默认"已经被拍摄"消息,而且似乎不喜欢它.
我收到此错误消息
Failure/Error: it { should validate_uniqueness_of(:name) }
Flavor did not properly validate that :name is case-sensitively unique.
Given an existing Flavor whose :name is ‹"Factory Flavor Namea"›,
after making a new Flavor and setting its :name to ‹"Factory Flavor
Namea"› as well, the matcher expected the new Flavor to be invalid and
to produce the validation error "has already been taken" on :name. The
record was indeed invalid, but it produced these validation errors
instead:
* name: ["This …Run Code Online (Sandbox Code Playgroud) activemodel ×10
rspec ×3
ruby ×3
activerecord ×2
mongodb ×1
mongoid ×1
mongoid6 ×1
mustache ×1
rails-i18n ×1
shoulda ×1
tdd ×1
templates ×1
validation ×1