标签: nested-attributes

在rails中对嵌套表单字段进行排序

我正在尝试创建一个库存表单,根据其类别对产品进行排序.理想情况下,我将能够按类别列出这些产品,并将添加一些javascript来隐藏/显示给定类别的产品.

目前,我无法解决如何在我的表单中按类别拆分产品的问题.这是我目前所拥有的(模仿Ryan Bates的嵌套属性的Railscasts):

class InventoriesController < ApplicationController
 def new
  @title = "Take Inventory"
  @vendor = ProductVendor.find(params[:product_vendor_id])
  @inventory = Inventory.new()
  @products = @vendor.products.active
   @products.each do |product|
    @inventory_line_item = @inventory.inventory_line_items.build({:product_id =>    product.id})
   end
 end
Run Code Online (Sandbox Code Playgroud)

我的新库存表格:

<%= form_for @inventory do |f| %>
 <table>
  <tr>
   <th>Item</th>
   <th>Quantity</th>
  </tr>

 <% f.fields_for :inventory_line_items do |builder| %>

 <tr>
  <td><%= builder.object.product.name %></td>
  <td><%= builder.text_field(:quantity, :size => 3) %></td>
      <%= builder.hidden_field :product_id %>
 </tr>

 <% end %>

 </table>
 <%= f.hidden_field(:user_id, :value => current_user.id) %>
 <%= f.hidden_field(:location_id, :value => current_location.id) …
Run Code Online (Sandbox Code Playgroud)

forms ruby-on-rails nested-attributes

4
推荐指数
1
解决办法
1819
查看次数

使用嵌套路由与accepts_nested_attributes_for有什么区别?

我可能完全混淆了两者,但我发现这些表单可以使用基于嵌套路由的数组参数来促进关联,例如:

<%= form_for [@project, @task]...
Run Code Online (Sandbox Code Playgroud)

或者fields_for如果父类accepts_nested_nested_attributes_for是孩子,则使用帮助器.

这些方法之间的区别/权衡是什么?

forms ruby-on-rails associations nested-attributes

4
推荐指数
1
解决办法
515
查看次数

Rails 3,多对多形式使用accepts_nested_attributes_for,如何正确设置?

我在食谱配料之间有多对多的关系.我正在尝试构建一个允许我在配方中添加成分的表单.

(这个问题的变体已被反复询问,我花了好几个小时,但基本上对于accepts_nested_attributes_for它的含义感到困惑.)

在您对以下所有代码感到害怕之前,我希望您会发现它确实是一个基本问题.这是非恐怖的细节......

错误

当我显示一个表单来创建一个食谱时,我收到错误"未初始化的常量Recipe :: IngredientsRecipe",指向我的表单中的一行

18:   <%= f.fields_for :ingredients do |i| %>
Run Code Online (Sandbox Code Playgroud)

如果我改变这一行,使"成分"单数

<%= f.fields_for :ingredient do |i| %>
Run Code Online (Sandbox Code Playgroud)

然后表单显示,但是当我保存时,我得到一个质量分配错误Can't mass-assign protected attributes: ingredient.

模型(3个文件,相应命名)

class Recipe < ActiveRecord::Base
  attr_accessible :name, :ingredient_id
  has_many :ingredients, :through => :ingredients_recipes
  has_many :ingredients_recipes

  accepts_nested_attributes_for :ingredients
  accepts_nested_attributes_for :ingredients_recipes
end

class Ingredient < ActiveRecord::Base
  attr_accessible :name, :recipe_id
  has_many :ingredients_recipes
  has_many :recipes, :through => :ingredients_recipes

  accepts_nested_attributes_for :recipes
  accepts_nested_attributes_for :ingredients_recipes
end

class IngredientsRecipes < ActiveRecord::Base
  belongs_to …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails has-many-through nested-attributes ruby-on-rails-3

4
推荐指数
1
解决办法
3711
查看次数

rails 4 collection_select多个嵌套属性未正确保存

我是rails的新手,我在使用collection_select保存连接表的嵌套属性时遇到问题.我有模型post,tag和post_tagging.post_tagging是一个连接表.

我想为每个帖子设置多个标签,所以我尝试通过collection_select使用多选,但是当我保存时,只将post_id插入到数据库中.下面是我的代码和日志.

Post.rb

class Post < ActiveRecord::Base
  has_many :post_taggings, foreign_key: :post_id, dependent: :destroy
  has_many :tags, through: :post_taggings, source: :tag
  accepts_nested_attributes_for :post_taggings, reject_if: :all_blank, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)

Tag.rb

class Tag < ActiveRecord::Base
  has_many :post_taggings, foreign_key: :tag_id, dependent: :destroy
  has_many :posts, through: :post_taggings, source: :post
end
Run Code Online (Sandbox Code Playgroud)

post_tagging.rb(我在post_tagging模型中关闭了tag_id和post_id的状态验证,因此我可以获得POST的日志.)

class PostTagging < ActiveRecord::Base
  belongs_to :post
  belongs_to :tag

  #validates :post_id, presence: true
  #validates :tag_id, presence: true
end
Run Code Online (Sandbox Code Playgroud)

posts_controller.rb(缩写)

class PostsController < ApplicationController
  def new
    @post = Post.new
    @post.post_taggings.build
  end


  def new_post_params
    params.require(:post).permit(:title, post_taggings_attributes: { :tag_id …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-attributes ruby-on-rails-4 collection-select

4
推荐指数
1
解决办法
3006
查看次数

Rails 4 - 带有accepts_nested_attributes_for控制器设置的嵌套表单?

我正在尝试使用form_for和制作嵌套表单fields_for.经过大量的研究和成功,不再在我的项目上工作了.我只是想重新创建一个railscast,看看我做错了什么.

我正在尝试重新创建在http://railscasts.com/episodes/196-nested-model-form-part-1上找到的示例,因为代码存在,所以不应该那么难,但我可以'设法从调查中创建问题.这是我的代码,直到现在:

rails new surveysays
rails g scaffold survey name:string
rake db:migrate
rails g scaffold 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, dependent: :destroy
  accepts_nested_attributes_for :questions
end
Run Code Online (Sandbox Code Playgroud)

我的调查表格带有嵌套问题字段:

<%= form_for(@survey) do |f| %>
      ...
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
      <%= f.fields_for :questions do |builder| %>
        <p>
          <%= builder.label :content, "Question" %><br/>
          <%= builder.text_area :content, :row …
Run Code Online (Sandbox Code Playgroud)

controller nested-forms nested-attributes ruby-on-rails-4

4
推荐指数
1
解决办法
1万
查看次数

使用 accepts_nested_attributes_for 为 FactoryGirl 对象生成嵌套属性

我正在使用 RSpec 和 Factory Girl 来测试我的应用程序。我正在尝试做的是以下内容:我有一个接受嵌套属性的对象,并且该嵌套属性无效。我想测试 POST 是否有效:

let(:valid_attributes) { build(:user).attributes }
it "creates a new User" do      
  expect {
    post :create, {user: valid_attributes}, valid_session
  }.to change(User, :count).by(1)
end
Run Code Online (Sandbox Code Playgroud)

那是工厂:

FactoryGirl.define do
  factory :user do |x|
    name "Homer"
    after(:build) do
      build :address
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

问题是由 返回的散列build(:user).attributes没有address,尽管如果我检查由 创建的对象build(:user)address是正确构建的。

有什么方法可以轻松生成带有嵌套属性的散列?

ruby rspec ruby-on-rails nested-attributes factory-bot

4
推荐指数
1
解决办法
1918
查看次数

验证表单中嵌套属性的存在

我有以下关联:

#models/contact.rb
class Contact < ActiveRecord::Base
  has_many :contacts_teams
  has_many :teams, through: :contacts

  accepts_nested_attributes_for :contacts_teams, allow_destroy: true
end

#models/contacts_team.rb
class ContactsTeam < ActiveRecord::Base
  belongs_to :contact
  belongs_to :team
end

#models/team.rb
class Team < ActiveRecord::Base
  has_many :contacts_team
  has_many :contacts, through: :contacts_teams
end
Run Code Online (Sandbox Code Playgroud)

Acontact应始终至少有一个关联的团队(在 的丰富联接表中指定contacts_teams)。

如果用户尝试创建没有关联团队的联系人:应该抛出验证。如果用户尝试删除联系人的所有关联团队:应引发验证。

我怎么做?

我确实查看了嵌套属性文档。我还看了这篇文章这篇文章,它们都有些过时了。

完成:我正在使用nested_form_fieldsgem 将新的关联团队动态添加到联系人。这是表单上的相关部分(有效,但目前无法验证是否至少有一个团队与联系人相关联):

<%= f.nested_fields_for :contacts_teams do |ff| %>
  <%= ff.remove_nested_fields_link %>
  <%= ff.label :team_id %>
  <%= ff.collection_select(:team_id, Team.all, :id, :name) %>
<% end %> …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-attributes

4
推荐指数
2
解决办法
7310
查看次数

Rails 5错误消息:Child-Model Parent-Model必须存在

我有两个型号,父母是财产,孩子是电话.尝试使用嵌套的Phone数据创建新的Property记录时,收到错误消息:Phones属性必须存在.

我已经研究过Rails指南和许多其他文档而没有确定原因.如果你想看到所有代码,这是一个公共github链接:https://github.com/allenroulston/testnest.git

class Property < ApplicationRecord
  has_many :phones
  accepts_nested_attributes_for :phones
end

class Phone < ApplicationRecord
  belongs_to :property
end

# the form accepting the data
<%= form_for(property) do |f| %>
  <% if property.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(property.errors.count, "error") %> prohibited this property from being saved:</h2>

      <ul>
      <% property.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= …
Run Code Online (Sandbox Code Playgroud)

nested-attributes ruby-on-rails-5

4
推荐指数
1
解决办法
2907
查看次数

如何在form_for中使用多个"一对多"嵌套属性

鉴于用户有很多信用卡而信用卡有很多地址这一事实,我正在尝试创建一个表格,创建一个用户和信用卡,地址全部一次

相关模型代码:

class User < ActiveRecord::Base
  has_many :credit_cards
  accepts_nested_attributes_for :credit_cards
end

class CreditCard < ActiveRecord::Base
  has_many :addresses
  accepts_nested_attributes_for :addresses
end
Run Code Online (Sandbox Code Playgroud)

控制器代码

def new
  @user = User.new
  @user.credit_cards.build
end
Run Code Online (Sandbox Code Playgroud)

查看代码

=form_for @user, :url => users_path do |u|
  =u.label :first_name, "Name"
  =u.text_field :first_name
    -u.fields_for :credit_cards do |cc|
      =cc.label :name_on_card, "Name on Card"
      =cc.text_field :name_on_card
      -cc.fields_for :address do |address|
        =address.label :address, "Address"
        =address.text_field :address1
Run Code Online (Sandbox Code Playgroud)

所以我遇到的问题是地址字段没有出现.我尝试添加@user.credit_cards.addresses.build到控制器,但我收到一个undefined method 'build' for nil错误.

ruby ruby-on-rails has-many form-for nested-attributes

3
推荐指数
1
解决办法
2097
查看次数

Rails - 验证嵌套属性唯一性与父级的范围父级

我在Rails中对父级父级的嵌套属性进行作用域唯一性验证时遇到问题.

背景

我有一个带有3个型号的rails 4应用程序:

#app/models/account.rb
class Account < ActiveRecord::Base
  has_many :contacts, dependent: :destroy
end

#app/models/contact.rb
class Contact < ActiveRecord::Base
  belongs_to :account
  has_many :email_addresses, dependent: :destroy, validate: :true, inverse_of: :contact
  accepts_nested_attributes_for :email_addresses,allow_destroy: true
  validates :email_addresses, presence: true
end

#app/models/email_address.rb
class EmailAddress  < ActiveRecord::Base
  belongs_to :contact, inverse_of: :email_addresses

  validates :label, presence: true
  validates :contact, presence: true
  validates :email, uniqueness: true, presence: true
  validates_email_format_of :email
end
Run Code Online (Sandbox Code Playgroud)

问题

我想创建一个范围,以确保模型EmailAddress 的属性:电子邮件在帐户级别是唯一的(帐户是Contact的父,它本身是EmailAddress的父级).

正如http://guides.rubyonrails.org/active_record_validations.html所建议的,我尝试过:

 class EmailAddress  < ActiveRecord::Base …
Run Code Online (Sandbox Code Playgroud)

scope ruby-on-rails validates-uniqueness-of nested-attributes

3
推荐指数
2
解决办法
7249
查看次数