Tag: 嵌套属性

Has_Many通过关联和嵌套属性

我正在尝试创建一个视图,允许我直接创建和编辑我的连接表的值。 这种模式被称为“雇佣”。 我需要能够在我的加入表中创建多行,以便孩子最多可以雇佣2本书。 我遇到了一些麻烦,我怀疑这取决于我的协会…… 我有3个型号。 每个孩子可以有2本书: class Child < ActiveRecord::Base has_many :hires has_many :books, through: :hires end class Hire < ActiveRecord::Base belongs_to :book belongs_to :child accepts_nested_attributes_for :book accepts_nested_attributes_for :child end class Book < ActiveRecord::Base has_many :hires has_many :children, through: :hires belongs_to :genres end 控制器看起来像这样: class HiresController < ApplicationController … def new @hire = Hire.new 2.times […]

如何在一个关系中使用accepts_nested_attributes_for设计?

我试图让我的用户表单也允许用户通过form_for同时填写他们的公司资料。 由于某种原因,它没有显示公司字段。 这是我的控制器和布局代码。 class User < ActiveRecord::Base attr_accessible :company_attributes has_one :company accepts_nested_attributes_for :company end class Company true end

Railsvalidation未在嵌套模型上运行

我在Rails 3.2.8和Ruby 1.9.3上。 我无法弄清楚为什么没有运行嵌套属性的validation或返回任何错误。 当我提交没有填写任何内容的表单时,我会为父模型(用户)收到错误,但不会为子模型(帐户)收到错误。 在我的下面的代码中,我有一个用户模型has_one owned_account(帐户模型),以及一个属于所有者的帐户模型(用户模型)。 Account模型具有子域字符串的文本字段。 看来,当我提交表单而不包含子域字段时,帐户模型上的validation根本不会运行。 关于如何在此处获得validation的任何想法? 提前感谢任何帮助或指示。 user.rb class User ‘Account’, :foreign_key => ‘owner_id’ validates_associated :owned_account accepts_nested_attributes_for :owned_account, :reject_if => proc { |attributes| attributes[‘subdomain’].blank? } end account.rb class Account ‘User’ validates :subdomain, :presence => true, :uniqueness => true, :format => { …some code… } end new.haml = form_for @user do |f| […]

如何validationrails 4中的嵌套属性字段?

我有两个型号 class Information < ActiveRecord::Base belongs_to :study validates_presence_of :email end 和 class Study < ActiveRecord::Base has_many :informations accepts_nested_attributes_for :informations end 我展示了一种研究forms,其中包含很少的信息字段,我想validation这些字段的存在。 只有在validation成功时我才想保存研究字段值,并且我想在validation失败时显示错误。 我怎样才能做到这一点? 提前致谢。

Rails嵌套表单,其中预定义字段为复选框

我正在制作一个包含Room和RoomAttribute模型的酒店应用程序。 这两个模型通过连接表在彼此之间具有many_to_many关系。 每个模型的属性如下: Room – room_number , room_type (例如“豪华”或“套房”)和price 。 RoomAttributes – name (例如“无线互联网”,“有线电视”,“浴缸”)。 用户将首先创建一组房间属性,以便每次创建新房间时都可以选择这些房间属性。 例如,有些房间可能有无线互联网,有些则没有。 app/views/rooms/new.html.erb的代码是(我对使用原始html表示歉意)。 <form action="” method=”post”> <input type="hidden" name="authenticity_token" value="”> Room Number: Type: Price: Attributes: <input type="checkbox" name="room[room_attributes_ids][]" value="”> 我正在使用Rails 4,我想就以下内容征求意见: 使用表单助手或formtastic设置表单,以便它已经输出所有预定义的房间属性,用户只需通过复选框选择要包含在房间中的属性。 编写RoomController#create方法,以便将嵌套的RoomAttribute模型设置为房间属性。 我的app/models/room.rb是否需要accepts_nested_attributes_for :room_attributes app/models/room.rb ? 如何在此方案中合并强参数。 我读过我应该用的 params.require(:room).permit(:room_number, :room_type, :price, room_attributes_attributes: [:id]) 但这不适合我。 谢谢! 🙂

嵌套属性无法使用新父级创建子级

我有两个型号: class Shift < ActiveRecord::Base attr_accessible :ranges_attributes has_many :ranges accepts_nested_attributes_for :ranges, allow_destroy: true end class Range < ActiveRecord::Base belongs_to :shift validates :shift, presence: true end 在我的控制器中,当我想创建一个带有范围的class次时: Shift.create! params[:shift] #ActiveRecord::RecordInvalid Exception: Validation failed: Shift ranges shift can’t be blank 如果我从Range模型中移除validates :shift, presence: true ,这可以很好地工作。 我能够和他的孩子一起创造一个新的转变。 ActiveRecord为我做到了这一点。 问题是: 为什么我需要删除该validation才能使其工作?

simple_fields_for没有显示

我试图创建两个隐藏的字段,一个显示没有问题,但另一个来自嵌套表单不会 product.rb class Product proc { |x| x[:option_name].blank? } belongs_to :user end product_option.rb class ProductOption < ActiveRecord::Base belongs_to :product end products_controller.rb class ProductsController @product_option.option_name) @current_user.update(:selected_product => @product.id) render :nothing => true end private def product_params params.require(:product).permit(:name, :id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, :product_options_attributes => [:id, :option_name, :ranking, :total_redeemed, :product_id]) end end _form.html.erb “post”,:remote => […]

添加accepts_nested_attributes_for时,Fields_for消失

我在Rails 3.2.5中做了一个嵌套的表单,但是当我添加accepts_nested_attributes_for我的fields_for消失时(它们只是停止在我的表单中显示)。 这是我的模特: class Product < ActiveRecord::Base attr_accessible :title, :description, :variants_attributes has_many :variants accepts_nested_attributes_for :variants validates :title, presence: true end 我的第二个模特是 class Variant < ActiveRecord::Base belongs_to :product attr_accessible :price, :sku, :weight, :inventory_quantity, :product_id end 在我看来,我有 {:multipart => true} do |f| %> Producto Nuevo ** ** _inventory_fields.html.erb **之间的部分是未打印的部分。 当我在我的产品模型fields_中删除accepts_nested_attributes_for时,再次开始显示,但我的表单不起作用。 发生了什么?!?!

如何在Rails 3中用Draper装饰嵌套属性(关联)?

我的环境: Rails 3.2 与gem擦肩而过 我正在使用嵌套资源,并且无法确定在何处声明装饰器。 #app/controllers/users_controller.rb def show @user = UserDecorator.find(params[:id]) @items = @user.items end #app/controllers/items_controller.rb def show @item = @user.items.find(params[:id]) end 我尝试用ItemDecorator替换items , ItemDecorator它没有用。 我应该把它放在哪里? 我知道Draper在表单中存在嵌套资源的问题,但这不是表单!

Rails 4不更新嵌套属性

问题:当我点击相关features_controller.rb的#update操作时,它们是在现有嵌套属性的基础上创建的 ,而不是更新嵌套属性。 可能的原因:我认为问题在于我在Rails的form_for缺乏理解。 我认为故障在我的视图中,我如何呈现持久的嵌套属性,和/或我如何指定嵌套属性的id失败,导致它只是创建一个新的id feature.rb class Feature < ActiveRecord::Base … has_many :scenarios accepts_nested_attributes_for :scenarios, allow_destroy: true, reject_if: :all_blank … end features_controller.rb def update … project = Project.find(params[:project_id]) @feature = Feature.find(params[:id]) if @feature.update_attributes(feature_params) # checking feature_params looks good… # feature_params[‘scenarios’] => { } redirect_to project else render :edit end end … private def feature_params params.require(:feature).permit(:title, :narrative, […]