我有一个非常简单的问题.但到目前为止还没有找到解决方案.
所以这是我发送给服务器的JSON字符串:
{
"name" : "abc",
"groundtruth" : {
"type" : "Point",
"coordinates" : [ 2.4, 6 ]
}
}
Run Code Online (Sandbox Code Playgroud)
使用新的许可方法,我得到:
params.require(:measurement).permit(:name, :groundtruth)
Run Code Online (Sandbox Code Playgroud)
这不会引发任何错误,但创建的数据库条目包含null而不是groundtruth值.
如果我只是设置:
params.require(:measurement).permit!
Run Code Online (Sandbox Code Playgroud)
所有东西都按预期保存,但当然,这会破坏强参数提供的安全性.
我找到了解决方案,如何允许数组,但没有找到使用嵌套对象的单个示例.这必须是某种可能的,因为它应该是一个非常常见的用例.那么它是怎样工作的?
ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4
我正在尝试关注Ryan Bates RailsCast#196:嵌套模型表单第1部分.Ryans版本有两个明显的区别:1)我正在使用内置脚手架而不是他正在使用的漂亮,2)我正在运行rails 4(我真的不知道Ryans在他的演员阵容中使用的是什么版本,但它不是4).
所以这就是我做的
rails new survey2
cd survey2
bundle install
rails generate scaffold survey name:string
rake db:migrate
rails generate model question survey_id:integer content:text
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
然后我将关联添加到模型中
class Question < ActiveRecord::Base
belongs_to :survey
end
Run Code Online (Sandbox Code Playgroud)
所以
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end
Run Code Online (Sandbox Code Playgroud)
然后我添加了嵌套视图部分
<%= form_for(@survey) do |f| %>
<!-- Standard rails 4 view stuff -->
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.fields_for :questions do |builder| %>
<div>
<%= …Run Code Online (Sandbox Code Playgroud) 关于这个基本问题我得到了各种相互矛盾的信息,答案对我目前的问题非常关键.因此,非常简单,在Rails 3中,允许或不允许将accepts_nested_attributes_for与belongs_to关系一起使用吗?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
Run Code Online (Sandbox Code Playgroud)
在一个视图中:
= form_for @user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
Run Code Online (Sandbox Code Playgroud) 我想建立一个多态关系accepts_nested_attributes_for.这是代码:
class Contact <ActiveRecord::Base
has_many :jobs, :as=>:client
end
class Job <ActiveRecord::Base
belongs_to :client, :polymorphic=>:true
accepts_nested_attributes_for :client
end
Run Code Online (Sandbox Code Playgroud)
当我试图访问Job.create(..., :client_attributes=>{...}给我NameError: uninitialized constant Job::Client
ruby ruby-on-rails polymorphic-associations nested-attributes
我有以下2个型号
class Sport < ActiveRecord::Base
has_many :charts, order: "sortWeight ASC"
has_one :product, :as => :productable
accepts_nested_attributes_for :product, :allow_destroy => true
end
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :productable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
如果没有产品,运动就不可能存在,所以sports_controller.rb我的拥有:
def new
@sport = Sport.new
@sport.product = Product.new
...
end
Run Code Online (Sandbox Code Playgroud)
我试图将产品的创建转移到运动模型,使用after_initialize:
after_initialize :create_product
def create_product
self.product = Product.new
end
Run Code Online (Sandbox Code Playgroud)
我很快就知道after_initialize只要模型被实例化(即来自一个find调用)就会被调用.所以这不是我想要的行为.
我应该如何建模所有人sport都有的要求product?
谢谢
我有两个型号.
- Parent has_many Children ;
- Parent accepts_nested_attributes_for Children ;
class Parent < ActiveRecord::Base
has_many :children, :dependent => :destroy
accepts_nested_attributes_for :children, :allow_destroy => true
validates :children, :presence => true
end
class Child < ActiveRecord::Base
belongs_to :parent
end
Run Code Online (Sandbox Code Playgroud)
我使用验证来验证每个父母的孩子的存在,所以我不能保存没有孩子的父母.
parent = Parent.new :name => "Jose"
parent.save
#=> false
parent.children_attributes = [{:name => "Pedro"}, {:name => "Emmy"}]
parent.save
#=> true
Run Code Online (Sandbox Code Playgroud)
验证工作.然后我们将通过_destroy属性摧毁孩子:
parent.children_attributes = {"0" => {:id => 0, :_destroy => true}}
parent.save
#=> true !!!
parent.reload.children
#=> []
Run Code Online (Sandbox Code Playgroud)
所以我可以通过嵌套表单销毁所有孩子,验证将通过. …
考虑以下关联:
class Product < ActiveRecord::Base
belongs_to :shop
accepts_nested_attributes_for :shop
end
Run Code Online (Sandbox Code Playgroud)
如果
params[:product][:shop_attributes] = {"name" => "My Shop"}
Run Code Online (Sandbox Code Playgroud)
我这样做:
@product = Product.new(params[:product])
@product.save
Run Code Online (Sandbox Code Playgroud)
@product正如预期的那样,创建一个名为"我的商店"的新商店并分配给它.
但是,我无法弄清楚当shop_attributes包含一些内容时会发生什么id,例如:
params[:product][:shop_attributes] = {"id" => "20", "name" => "My Shop"}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Couldn't find Shop with ID=20 for Product with ID=
Run Code Online (Sandbox Code Playgroud)
问题1
这意味着什么?
问题2
如果是这种情况,即id商店已知,并且商店id已经存在,我该如何创建@product这样的商店将被分配给它?
我似乎无法belongs_to使用accepts_nested_attributes_forRails 2.3 的新工具在rails视图中生成嵌套表单以获取关系.我确实检查了许多可用的资源,看起来我的代码应该正常工作,但是fields_for爆炸对我来说,我怀疑它与我如何配置嵌套模型有关.
我遇到的错误是一个常见的错误,可能有很多原因:
'@account[owner]' is not allowed as an instance variable name
Run Code Online (Sandbox Code Playgroud)
以下是涉及的两个模型:
class Account < ActiveRecord::Base
# Relationships
belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
accepts_nested_attributes_for :owner
has_many :users
end
class User < ActiveRecord::Base
belongs_to :account
end
Run Code Online (Sandbox Code Playgroud)
也许这就是我在做'rong'的地方,因为帐户可以拥有'所有者',可能是'用户',但用户只有一个'帐户',基于用户模型account_id键.
这是new.html.haml中的视图代码,它炸毁了我:
- form_for :account, :url => account_path do |account|
= account.text_field :name
- account.fields_for :owner do |owner|
= owner.text_field :name
Run Code Online (Sandbox Code Playgroud)
这是新操作的控制器代码:
class AccountsController < ApplicationController
# GET /account/new
def new
@account …Run Code Online (Sandbox Code Playgroud) 更新代码格式以便更好地查看.
伙计们,
我一直在看这个,但我不明白这里有什么可能搞乱的.我正在使用Devise.
class User < ActiveRecord::Base
has_many :addresses
accepts_nested_attributes_for :addresses
# Other stuff here
end
class Address < ActiveRecord::Base
belongs_to :user
validates_presence_of :zip #:street_address1,
Run Code Online (Sandbox Code Playgroud)
结束
--------------------日志输出开始--------------------------- ---
在2011-05-28 11:43:27 -0700开始POST"/ users"for 127.0.0.1由RegistrationsController处理#create as HTML参数:{"utf8"=>"√","authenticity_token"=>"CEmdqlsmdYa6Jq0iIf5KAxxISsUCREIrFNXWkP80nhk = ",""user"=> {"email"=>"a2 @ gmail.com","password"=>"[FILT ERED]","addresses_attributes"=> {"0"=> {"street_address1"=> "234 Pitkin Ct.","zip"=>"12456"}}},"commit"=>"注册"}警告:无法批量分配受保护的属性:addresses_attributes SQL(0.0ms)BEGIN SQL(164.0) ms)SHOW TABLES
用户加载(0.0ms)SELECTusers.idFROMusersWHERE(users.--------------------日志输出结束--------------------------- ---
zip已存在于发布的数据中,并且发布的数据似乎已正确格式化.在网页表单上,我收到"地址zip不能为空"的错误.我已经挖了一下导致"无法大量分配受保护属性"的警告,但没有找到任何可以帮助我的东西.
感谢您的想法和指示.
-S
我有一个用户和嵌套的配置文件类,如下所示:
class User < ActiveRecord::Base
has_one :profile
attr_accessible :profile_attributes
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :name
end
user = User.find(1)
user.profile.id # => 1
user.update_attributes(profile_attributes: {name: 'some name'})
user.profile.id # => 2
Run Code Online (Sandbox Code Playgroud)
我不明白为什么rails会丢弃旧配置文件并创建一个新配置文件.
运用
user.profile.update_attributes({name: 'some name'})
Run Code Online (Sandbox Code Playgroud)
只是按预期更新当前配置文件.但在那种情况下,我没有利用accepts_nested_attributes_for
有谁知道为什么更新会这样发生?我不希望最终得到一个没有连接到任何用户的配置文件行数据库.