Tag: 嵌套表单

嵌套模型validation – 不显示错误

关于这一点有很多问题,但它们似乎都没有帮助。 是的,我看过这个导演 。 我有一个有很多书的作者,如下: 作者: class Author < ActiveRecord::Base attr_accessible :name has_many :books, dependent: :destroy accepts_nested_attributes_for :books, allow_destroy: true validates :name, presence: true validates :name, length: { minimum: 3 } end 书: class Book < ActiveRecord::Base attr_accessible :name, :year belongs_to :author validates :name, :year, presence: true validates :year, numericality: { only_integer: true, less_than_or_equal_to: Time.now.year } […]

Rails 4:使用Cocoon Gem将child_index添加到动态添加(嵌套)表单字段

更新:我正在尝试将表单字段添加/删除到涉及多个模型的嵌套表单。 我已经看过Ryan Bates的“动态表格”轨道广播,我已经使用Cocoon Gem参考了这篇文章 。 在该文章之后,除了child_index之外,一切都完美无缺。 child_index仅出现在第一个:kid输入字段( :name )和第一个:pet输入字段( :name和:age )上。 然后它返回到正在添加的字段的真实性标记。 我已经删除了所有的JS和帮助器方法,而是使用了一些内置JS的Cocoon方法。 我修复了问题,单击“添加”将通过从application.html.erb文件中删除= javascript_include_tag :cocoon来添加两个字段而不是一个。 我试过添加jQuery和表单助手,但我不确定我是否正确输入了代码。 (我更改了模型对象以使关系更清晰) parent.rb文件: class Parent < ActiveRecord::Base has_many :kids has_many :pets, through: :kids # <<<<<< ADDED KIDS USING 'through:' kid.rb文件: class Kid < ActiveRecord::Base belongs_to :parent has_many :pets accepts_nested_attributes_for :pets, reject_if: :all_blank, allow_destroy: true validates :name, presence: true pet.rb文件: […]

params.require()。permit不能按预期工作

我有这个控制器 class PeopleController < ApplicationController def new @person = Person.new @person.phones.new end # this is the action that gets called by the form def create render text: person_params.inspect # @person = Person.new(person_params) # @person.save # redirect_to people_path end def index @person = Person.all end private def person_params params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ]) end end […]

Rails – 我无法使用嵌套表单在我的用户表单上显示位置

我无法使用嵌套表单显示belogns给用户的位置。 我想创建用户并在位置模型中保存地址,城市,州等,并在用户模型中保存名称,用户名和电子邮件。 我在控制台上没有错误。 但是字段(选择或下拉列表,text_fields等)没有显示出来。 数据库有记录。 这是我的user.rb模型: class User < ActiveRecord::Base has_many :locations accepts_nested_attributes_for :locations end 这是我的location.rb模型: class Location < ActiveRecord::Base belongs_to :user end 这是我的用户表单: prohibited this user from being saved:

在nested_form中的Rails nested_form

我有以下模型和关系: Rate fields t.string :type t.string :name class Rate < ActiveRecord::Base has_many :category_rate_requests end CategoryRateRequests fields t.date :date_from t.date :date_to class CategoryRateRequests < ActiveRecord::Base belongs_to :rate has_many :category_rates end CategoryRate t.integer :room_category_id t.integer :new_rate_id t.integer :category_rate_request_id t.integer :amount class CategoryRate < ActiveRecord::Base belongs_to :rate belongs_to :category_rate_request belongs_to :room_category end 我试图在nested_form中有一个nested_form = nested_form_for @rate do |f| […]

编辑表单删除数据库中的记录

我有一个嵌套的表单,可以很好地保存数据库的内容。 唯一的问题是,当我单击编辑时,“grand-child”字段将从数据库中删除,因此我必须重新输入该字段上的所有内容。 正常行为是编辑应存在的先前内容。 我正在使用gem cocoon simple_forms用于我的表单的嵌套表单simple_forms和用于ckeditor编辑的ckeditor 。 这是我单击编辑时的服务器日志:通知: SQL (0.3ms) DELETE FROM “responses” WHERE “responses”.”id” = $1 [[“id”, 53]]每次我点击编辑时都会发生进入考试模型。 另请注意,问题已选中但未删除。 很奇怪。 Started GET “/exams/24/edit” for 127.0.0.1 at 2015-11-09 16:59:40 +0300 Processing by ExamsController#edit as HTML Parameters: {“id”=>”24”} Exam Load (0.8ms) SELECT “exams”.* FROM “exams” WHERE “exams”.”id” = $1 LIMIT 1 [[“id”, 24]] Question Load (0.4ms) […]

如何使用jquery无法正常工作来限制嵌套表单字段

我正在关注嵌套表单字段中的Ryan Bates剧集,并添加了第2部分末尾建议的jquery。一切正常(我可以添加字段和删除字段)。 我现在想限制您可以在表单中添加的字段数。 在我的application.js我有 function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp(“new_” + association, “g”) $(link).parent().before(content.replace(regexp, new_id)); } 正如莱恩贝茨写的那样。 阅读另一篇文章我改变了现在的行: function add_fields(link, association, content) { if($(“.fields input”).length < 5) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).parent().before(content.replace(regexp, new_id)); } } 然而,这不起作用,我在这里做错了什么。 […]

如何在不通过表单传递数据的情况下更新嵌套资源

楷模 class User :destroy, :inverse_of => :user has_many :companies, :through => :roles accepts_nested_attributes_for :roles, :limit => 1, :allow_destroy => true end class Role :roles belongs_to :company, :inverse_of => :roles accepts_nested_attributes_for :company end class Company :destroy, :inverse_of => :user has_many :users, :through => :roles validates :name, presence: true end 自定义设计注册控制器 class RegistrationsController < Devise::RegistrationsController # GET […]

Active Admin中的嵌套表单

我需要帮助! 我有2个模型用于调查: class Poll :destroy accepts_nested_attributes_for :poll_questions, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true end 问题的模型如下:(似乎这些关联是正确的) class PollQuestion :destroy accepts_nested_attributes_for :poll_answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true end 另外在活动管理员中: ActiveAdmin.register Poll do form do |f| f.inputs “Main poll” do f.input :title f.input :description end f.inputs do f.has_many :poll_questions […]

Rails 4:保存子记录ID但不保存其属性,使用带有has_many关系的嵌套表单

我有3个模型与has_many通关系:食物(例如:巧克力),Sub(巧克力食物替代品),关节(联合表)。 说@food = Food.find(1); has_many通过关系允许我做@subs = @ food.subs,它返回与@food相关的所有替代品。 这工作正常,但只保存Sub id而不是它的属性:name和:description,因为你可以看到它在我的控制器中的create action中尝试保存@ food.subs时返回nil: => #<ActiveRecord::Associations::CollectionProxy [#]> 我想问题在于我的食物控制器中的创建动作,也许与我的嵌套forms有关。 我花了无数个小时试图解决这个问题,我非常渴望找到答案。 我真的不知道在哪里看。 我是rails的新手,所以非常感谢你的帮助和时间,我真的很感激。 请尽可能根据我的初学者水平调整你的答案:-)。 下面是我的控制器样品,表格和相关信息。 这是我的模特: class Food :joints accepts_nested_attributes_for :subs end class Sub :joints accepts_nested_attributes_for :foods end class Joint < ActiveRecord::Base belongs_to :food belongs_to :sub end 这是我的db-schema FYI: create_table “foods”, force: true do |t| t.string “name” t.text “description” […]