Tag: 嵌套表单

Rails 4:accepted_nested_attributes_for和质量分配

我试图在Rails 4中重现railscast#196 。但是,我遇到了一些问题。 在我的例子中,我尝试生成一个电话簿 – 每个人可以有多个PhoneNumbers 这些是我的控制器的重要部分: class PeopleController < ApplicationController def new @person = Person.new 3.times{ @person.phones.build } end def create @person = Person.create(person_params) @person.phones.build(params[:person][:phones]) redirect_to people_path end private def person_params params.require(:person).permit(:id, :name, phones_attributes: [ :id, :number ]) end end 这是我的新观点 New Person 不用说我有has_many :phones和accepts_nested_attributes_for :phones在我的人模型和belongs_to :person手机模型中的人。 我有以下问题: 而不是3个电话号码字段,新表单中只有一个 当我提交表单时,我收到一个错误: ::加载ActiveModel ForbiddenAttributesError 在线 @person.phones.build(params[:person][:phones]) […]

Rails 3,嵌套的多级表单和has_many通过

我试图让它工作,但它不是! 我有 class User :event_users has_many :event_users accepts_nested_attributes_for :event_users end class Event :event_users accepts_nested_attributes_for :users end class EventUser < ActiveRecord::Base set_table_name :events_users belongs_to :event belongs_to :user accepts_nested_attributes_for :events accepts_nested_attributes_for :users end 还有表格布局 event_users user_id event_id user_type events id name users id name 这是我的forms ‘participating’ %> 问题是如果我以这种方式创建一个新用户,它不会设置user_type的值(但它会创建一个用户和一个带有user_id和event_id的event_users)。 如果我在创建用户并提交后返回编辑表单,则在events_users中设置user_type的值。 (我也尝试过没有formtastic)任何建议? 谢谢! – – 编辑 – – 我也尝试过在用户之前使用event_users […]

Rails has_many通过表单与连接模型中的复选框和额外字段

我正试图解决一个非常常见的(我认为)任务。 有三种型号: class Product :categorizations accepts_nested_attributes_for :categorizations end class Categorization < ActiveRecord::Base belongs_to :product belongs_to :category validates :description, presence: true # note the additional field here end class Category < ActiveRecord::Base validates :name, presence: true end 我的问题始于产品新/编辑表格。 在创建产品时,我需要检查它所属的类别(通过复选框)。 我知道可以通过创建名称为’product [category_ids] []’的复选框来完成。 但是我还需要输入每个已检查关系的描述,这些关系将存储在连接模型(Categorization)中。 我在复杂的表格,habtm复选框等上看到了那些漂亮的Railscast。我一直在寻找StackOverflow。 但我没有成功。 我发现一篇文章描述了与我几乎完全相同的问题。 最后的答案对我来说是有道理的(看起来这是正确的方法)。 但它实际上并不是很好(即如果validation失败)。 我希望类别总是以相同的顺序显示(在新的/编辑表单中;在validation之前/之后)和复选框,以便在validation失败时保持原样等等。 任何人都赞赏。 我是Rails的新手(从CakePHP切换)所以请耐心等待,尽可能详细地写。 请指出我正确的方式! 谢谢。 🙂

Rails 4.0 with Devise。 嵌套属性Unpermited参数

我正在使用Devise和Rails 4开发一个Web应用程序。我有一个用户模型,我已经扩展了2个额外的表单字段,这样当用户注册时,他也可以提交他的名字/姓氏。 (基于http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/ )。 我现在想要添加一个Institution模型。 此模型has_many :用户和用户belongs_to :institution。 我希望能够在注册用户的同一表格上注册该机构的名称 。 我知道我需要在我的Institution模型中使用nested_attribute,因为这是父类,我将稍微展示一下。 当我尝试注册用户时,我进入控制台: Unpermited参数:机构 。 我的提示是我无法根据我的子类(User)更新我的父类(Institution)。 可能有解决方案吗? 或者有没有人经历类似的事情? class Institutions < ActiveRecord::Base has_many :users, accepts_nested_attributes_for :users end class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable belongs_to :institution end registrations / new.html.erb这里我有嵌套表格 resource_name, :url => registration_path(resource_name)) do |f| %> . . 基于我之前链接的教程,我创建了一个新的User :: ParameterSanitizer […]

使用rails嵌套模型来创建*外部对象并同时*编辑*现有的嵌套对象?

使用Rails 2.3.8 目标是创建一个Blogger,同时更新嵌套的User模型(如果信息已更改等),或者创建一个全新的用户(如果它尚不存在)。 模型: class Blogger < ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user end Blogger控制器: def new @blogger = Blogger.new if user = self.get_user_from_session @blogger.user = user else @blogger.build_user end # get_user_from_session returns existing user # saved in session (if there is one) end def create @blogger = Blogger.new(params[:blogger]) # … end 形成: # … other […]

Rails – 单个表单上多个模型的用户输入 – 如何

这基本上是一个嵌套的表单问题,尽管只有一个属于父模型的字段。 我的数据输入表单收集模型的数据 – 但是我还需要收集一个实际进入将使用详细记录创建的父记录的数据元素/值(UserID)。 AFAIK Rails希望每个表单字段都映射到一个模型,我需要创建一个我将单独使用的未绑定数据输入字段。 如何覆盖此默认行为并创建“自由格式/未绑定字段”? TIA,BC

已销毁的嵌套模型轨道中的validates_uniqueness_of

我有一个Project模型,它接受Task的嵌套属性。 class Project :true end class Task < ActiveRecord::Base validates_uniqueness_of :name end 任务模型中的唯一性validation在更新Project时会出现问题。 在编辑项目时,我删除任务T1,然后添加一个同名T1的新任务,唯一性validation限制了项目的保存。 params hash看起来像 task_attributes => { {“id” => “1”,”name” => “T1”, “_destroy” => “1”},{“name” => “T1”}} 在销毁旧任务之前完成对任务的validation。 因此validation失败。任何想法如何validation它不会认为任务被销毁?

Ruby on Rails嵌套属性没有保存到数据库中?

我正在尝试在“Todo列表”中创建项目列表,但是,我不确定我是否正确使用嵌套属性执行此操作。 我认为使用嵌套属性是正确的尝试,因为会有一个大的项目列表,并且它将与基于ids的正确“待办事项列表”相关联。 填充记录时表格的示例 待办事项表 id list 1 grocery shopping 2 health insurance 物品表 id todo_id name 1 1 buy milk 2 1 buy cereal 3 2 Blue Shield 4 2 Healthnet 5 1 buy cherries 虽然我的尝试如下,但我的应用程序没有将任何数据保存到Item数据库中。 Todo控制器 class TodoController < ApplicationController def new @todo = Todo.new @todo.items.build end end Todo模型 class Todo < ActiveRecord::Base belongs_to […]

关联模型和嵌套表单,validation不起作用

Update2:我已经清理了代码,这似乎已经解决了一些问题。 我在这里发布了新代码作为一个新问题。 更新:组织和用户有1:多关系。 我的问题涉及一个联合注册表单,其中需要组织和用户。 在maxcal对原始post的帮助之后,我为我的嵌套表单编写了一个新的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”} 。 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 […]

使用dm-accepts_nested_attributes和dm-is-tree在rails中的2个模型的嵌套表单

我有两个模型:论坛应用程序中的post和图像,其中post使用dm-is-tree以父子格式排列。 在这一点上,图像已成为Post模型的一部分。 由于Post模型变得笨拙并且我需要添加更多深度来表示图像,我正在努力将Image分离到它自己的模型中,但仍然是输出中的post的一部分。 所以我开始以简单的方式集成dm-accepts_nested_attributes: class Post include DataMapper::Resource property :id, Serial property :istop, String property :created_at, DateTime property :updated_at, DateTime property :content, Text has n, :images accepts_nested_attributes_for :images is :tree, :order => [:istop, :created_at] class Image include DataMapper::Resource property :id, Serial property :created_at, DateTime belongs_to :post property :image, String, :auto_validation => false # Carrierwave image […]