Tag: 嵌套属性

Rails 3.1+嵌套表单问题:无法批量分配受保护的属性

我有一个篮球应用程序,其中一个名单有很多玩家,一个玩家可以在多个名单上。(多对多的原因是玩家 – 名册档案) Roster.rb class Roster :rosterizes accepts_nested_attributes_for :players attr_accessible :jersey_number, :team_id, :class_year, :players end Rosterizes.rb (名字不好我知道……) class Rosterize < ActiveRecord::Base belongs_to :player belongs_to :roster attr_accessible :player_id, :roster_id end Player.rb class Player :rosterizes validates_presence_of :first_name, :last_name attr_accessible :first_name, :last_name, :active end 风景 New Player player_added_team_path, :html => { :class => ‘form-horizontal’ } do |f| %> […]

accepts_nested_attributes_for链接到现有记录,而不是创建新记录

我有以下型号 class Order < AR::Base has_many :products accepts_nested_attributes_for :products end class Product < AR::Base belongs_to :order has_and_belongs_to_many :stores accepts_nested_attributes_for :stores end class Store < AR::Base has_and_belongs_to_many :products end 现在我有一个订单视图,我想更新产品的商店。 问题是我只想将产品连接到我的数据库中的现有商店,而不是创建新商店。 我在订单视图中的表单看起来像这样(使用Formtastic): = semantic_form_for @order do |f| = f.inputs :for => :live_products do |live_products_form| = live_products_form.inputs :for => :stores do |stores_form| = stores_form.input :name, :as => […]

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 + JQuery-File-Upload +嵌套模型

我一直在寻找一些例子,但是很简单: 我正在尝试在我正在处理的项目上实现JQuery-File-Upload,但是我很想知道如何使用嵌套属性。 快速概述: 2型号: Comment [has_many :attachments] Attachment [belongs_to :comment] 评论accepted_nested_attributes_for :attachments 。 另外 – 我正在使用Dragonfly。 我已经回顾了JQuery-File-Upload网站上的Rails 3指南,但是他们认为它是一个单一的模型,所以它都是围绕一个表单构建的。 有没有人有任何他们的实现的例子,还是有一个我还没有偶然发现的现有教程? 我确定有人有类似的问题…是JQuery-File-Upload到适当的工具还是我应该看看别的什么?

accepted_nested_attributes_for with has_many =>:通过选项

我有两个模型,链接和标签,通过第三个link_tags关联。 以下代码位于我的链接模型中。 协会: class Link :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end class Tag :link_tags has_many :link_tags end class LinkTag < ActiveRecord::Base belongs_to :link belongs_to :tag end links_controller动作: def new @link = @current_user.links.build respond_to do |format| format.html # new.html.erb format.xml { […]

我应该如何将rails和simple_form用于嵌套资源?

我正在尝试同时使用另一个(嵌套)创建一个资源。 我正在使用Rails4和simple_form 3.0.0rc。 这是我的代码。 楷模: class User < ActiveRecord::Base has_one :profile accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user end 控制器: class UsersController < ApplicationController def new @user = User.new @user.build_profile end def create user = User.new user_params user.save redirect_to root_url # @par =params end private def user_params params.require(:user).permit(:email, profile_attributes: [:name]) end end 查看(新用户的表单) […]

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 […]

处理JSON时如何使用nested_attributes?

我正在尝试编写一个处理JSON的更新方法。 JSON看起来像这样: { “organization”: { “id”: 1, “nodes”: [ { “id”: 1, “title”: “Hello”, “description”: “My description.” }, { “id”: 101, “title”: “fdhgh”, “description”: “My description.” } ] } } 组织模式: has_many :nodes accepts_nested_attributes_for :nodes, reject_if: :new_record? 组织序列化器: attributes :id has_many :nodes 节点序列化器: attributes :id, :title, :description 组织控制器中的更新方法: def update organization = Organization.find(params[:id]) if organization.update_attributes(nodes_attributes: […]

Rails嵌套属性被复制

这个问题打败了我。 我整个周末一直在这里,但无法弄清楚发生了什么。 当我创建一个新的employee对象时,我accepts_nested_attributes_for :ee_pay在我的employee模型中使用accepts_nested_attributes_for :ee_pay创建多个新的ee_pay记录。 为新员工创建ee_pay记录取决于员工所属公司的company_pay对象数量。 所以在下面的例子中有2个company_pay对象 – >“Basic”[id:2]和“Time + 1/2”[id:3](在before_action返回)并且我想为每个对象创建一个ee_pay有问题的员工 这是代码: – employees_controller.rb before_action :set_company_pay_types, only: [:new, :update, :edit] def new @ee_pay_types = [] @taxable_pays.each do |pt| @ee_pay_types << @employee.ee_pay.build(company_pay_id: pt.id) end end private def set_company_pay_types @taxable_pays = CompanyPay.where(['company_id = ? AND pay_sub_head_id = ? AND description ?’, current_company.id, 1, “Salary”]).order(:id) end def employee_params […]