如何为所有RoR模型将include_root_in_json设置为false?
我试图在application.rb中设置ActiveModel :: Base.include_root_in_json = false,但它没有任何效果.
我正在尝试为我的模型使用ActiveModel而不是ActiveRecord,因为我不希望我的模型与数据库有任何关系.
以下是我的模型:
class User
include ActiveModel::Validations
validates :name, :presence => true
validates :email, :presence => true
validates :password, :presence => true, :confirmation => true
attr_accessor :name, :email, :password, :salt
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
@password = attributes[:password]
@password_confirmation = attributes[:password_confirmation]
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
class UsersController < ApplicationController
def new
@user = User.new
@title = "Sign up"
end
end
Run Code Online (Sandbox Code Playgroud)
我的观点是:
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field …Run Code Online (Sandbox Code Playgroud) 作为这个问题的前言:我对Rails开发(以及一般的Web开发)是全新的,我的一些担忧可能没有根据,所以任何反馈都会非常有用.
我正在尝试创建一个插入RESTful API的Rails应用程序.我一直在努力确定最好的方法,从我的理解,它缩小到从头开始制作我自己的模型,利用ActiveModel,或利用ActiveResource.
我不清楚每个的优点/缺点,坦率地说,我还没有完全理解ActiveModel和ActiveResource之间的区别.有人能否为我提供有关这三种选择的见解以及在最佳环境中最具"意义"的内容?谢谢!
最好的答案不仅仅是说"使用ActiveModel"或"使用ActiveResource"以及这样做的说明,但这也会有所帮助.我真的很感激答案解释为什么我应该使用那个东西,等等.
我正在处理的一些约束是我在调用API时需要使用密钥,并且大量的API调用将包含其他参数.
我想有一个仪表板来显示多个模型的摘要,我使用Presenter实现它而没有自己的数据.我使用ActiveModel类(没有数据表):
class Dashboard
attr_accessor :user_id
def initialize(id)
self.user_id = id
end
delegate :username, :password, :to => :user
delegate :address, :to => :account
delegate :friends, :to => :friendship
end
Run Code Online (Sandbox Code Playgroud)
通过代表,我希望能够打电话Dashboard.address回来Account.find_by_user_id(Dashboard.user_id).address.
如果Dashboard是一个ActiveRecord类,那么我可以声明Dashboard#belongs_to :account并且委托会自动工作(即,Account会知道它应该从Dashboard实例中的user_idequals 返回地址属性to user_id).
但Dashboard不是ActiveRecord类,所以我不能声明belongs_to.我需要另一种方法来告诉Account查找正确的记录.
有办法克服这个问题吗?(我知道我可以假装Dashboard有一个空表,或者我可以将User的实例方法重写为带参数的类方法.但这些解决方案都是黑客攻击).
谢谢.
我正在开发一个类似DataMapper的小型ODM项目,我正在尝试使用该ActiveModel::Validations组件.但是,我在编写测试时遇到了问题 - 我使用匿名类来构造我的测试模式,但是当运行验证器时,ActiveModel::Name类会抛出错误:
Class name cannot be blank. You need to supply a name argument when anonymous class given
这是一个重现的简单代码示例:
require 'active_model'
book_class = Class.new do
include ActiveModel::Validations
validates_presence_of :title
def title; ""; end # This will fail validation
end
book_class.new.valid? # => throws error
Run Code Online (Sandbox Code Playgroud)
只有在验证程序失败时才会引发异常 - 我猜测在尝试构造验证错误消息时会发生问题.所以我的问题是:
在Bryan Helmkamp的优秀博客文章" 7个模式来重构Fat ActiveRecord模型 "中,他提到使用Form Objects抽象多层形式并停止使用accepts_nested_attributes_for.
编辑:请参阅下面的解决方案.
我几乎完全复制了他的代码示例,因为我有同样的问题需要解决:
class Signup
include Virtus
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
attr_reader :user
attr_reader :account
attribute :name, String
attribute :account_name, String
attribute :email, String
validates :email, presence: true
validates :account_name,
uniqueness: { case_sensitive: false },
length: 3..40,
format: { with: /^([a-z0-9\-]+)$/i }
# Forms are never themselves persisted
def persisted?
false
end
def save
if valid?
persist!
true
else
false
end
end
private
def persist!
@account = …Run Code Online (Sandbox Code Playgroud) 我正在尝试发送我的前端应用程序json,如下所示:
{
facilities: [
{id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]}
],
instructors: [
{id: 4, name: 'Johnny Pheonix', skill: '8', picture: 'aws_url', facility_ids: [5, 8, 12}
]
}
Run Code Online (Sandbox Code Playgroud)
render :json => @facilities
Run Code Online (Sandbox Code Playgroud)
串行器发现了这一点.好极了!但这并不包括任何教师
render :json => {facilities: @facilities, instructors: @instructors}
Run Code Online (Sandbox Code Playgroud)
这给了我一个教师数组和一个工具数组,但是没有使用activeModel :: Serializers.
render :json => [@facilities, @instructors]
Run Code Online (Sandbox Code Playgroud)
起初我很兴奋这个,因为它给了我两个数组,它使用了ActiveModel :: Serializers.但是,这就是JSON的样子:
{facilities: [
{facilities: [
#my facilities data
]},
{facilities: [
#my instructor data
]}
]}
Run Code Online (Sandbox Code Playgroud)
json activemodel ruby-on-rails-3 ember.js active-model-serializers
我使用Rails 3.0.4和RSpec 2.5.例如,在我的控制器中,我大量使用命名范围
@collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate(
:page => params[:page], :conditions => { ... })
在我的测试中,我希望能够模拟这种查询的结果,而不是措辞.我觉得做某事并不合理
GuestbookEntry.stub_chain(:nonreplies, :bydate, ...).and_return(...)
因为当我决定重新排序命名范围时,此测试将失败.
使用Rails 2.3和RSpec 1.x,这很好用:我可以写
GuestbookEntry.should_receive(:find).with(:all, :conditions => { ... })
并且上述调用将被捕获并正确处理.然而,使用Rails 3,由于某种原因,这不再起作用.
为什么?如何设置嵌套范围结果的期望值或存根?由于Rails 3的ActiveModel中的所有内容都是命名范围(感谢ARel),因此必须以某种方式实现,否则测试确实非常脆弱.
谢谢!
更新:另请参阅GitHub上的问题报告.
我有以下型号:
class Programme < ActiveRecord::Base
has_and_belongs_to_many :nationalities, class_name: 'Nation', join_table: 'nationalities_nations'
has_and_belongs_to_many :destinations, class_name: 'Nation', join_table: 'destinations_nations'
accepts_nested_attributes_for :nationalities
accepts_nested_attributes_for :destinations
end
Run Code Online (Sandbox Code Playgroud)
和
class Nation < ActiveRecord::Base
has_and_belongs_to_many :nationality_programmes, class_name: 'Programme', join_table: 'nationalities_nations'
has_and_belongs_to_many :destination_programmes, class_name: 'Programme', join_table: 'destinations_nations'
accepts_nested_attributes_for :nationality_programmes
accepts_nested_attributes_for :destination_programmes
end
Run Code Online (Sandbox Code Playgroud)
在活动管理员中,我有以下配置,可以正确地预选任何现有的存储国家/地区参考(请参见屏幕截图).
ActiveAdmin.register Programme do
permit_params :title,
destinations_ids: [:id],
nationalities_ids: [:id]
form do |f|
f.actions
f.inputs 'Countries / Regions' do
f.input :nationalities, :as => :select, :input_html => {:multiple => true}
f.input :destinations, :as => :select, :input_html => …Run Code Online (Sandbox Code Playgroud) 我有一个嵌套属性,我在其上执行状态验证.我正在尝试没有成功提供完整错误消息文本中返回的属性名称的翻译.
调用模型Identity并包含一个名为identity的模型.模型嵌套在另一个具有has_many关系的模型中.
当前返回典型的错误消息
Identities identity can't be blank
Run Code Online (Sandbox Code Playgroud)
我想将属性(默认情况下Identities identity)转换为其他内容.
我有
en:
activerecord:
models:
identity:
identity: "whatever"
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我会收到错误
I18n::InvalidPluralizationData (translation data {:identity=>"whatever"} can not be used with :count => 1):
Run Code Online (Sandbox Code Playgroud)
我试图通过改变上面的内容来为此添加复数数据
en:
activerecord:
models:
identity:
identity:
one: "one"
other: "other"
Run Code Online (Sandbox Code Playgroud)
这会将错误更改为
I18n::InvalidPluralizationData (translation data {:identity=>{:one=>"one", :other=>"other"}} can not be used with :count => 1):
Run Code Online (Sandbox Code Playgroud)
我也试过many而不是other没有区别.
我已经花了几个小时试图完成这项工作,在Stack Overflow和其他地方阅读了其他问题但没有成功.为属性名称编写翻译的正确方法是什么?
activemodel ×10
json ×2
ruby ×2
activeadmin ×1
activerecord ×1
arel ×1
bdd ×1
ember.js ×1
presenter ×1
rails-i18n ×1
rspec2 ×1
validation ×1