标签: nested-forms

rails accepts_nested_attributes_for:reject_if无效

我试图覆盖autosave参数,因为我认为它无法完成.
我把它has_shipping_address从模型转移OrderShippingAddress模型,现在我有:

#the models..
class Order < ActiveRecord::Base

  belongs_to :billing_address
  belongs_to :shipping_address 

  accepts_nested_attributes_for :billing_address
  accepts_nested_attributes_for :shipping_address, :reject_if => proc { |attributes| attributes["has_shipping_address"] != '1' }

  def after_initialize                       
    self.build_billing_address unless billing_address
    self.build_shipping_address unless shipping_address
  end

end

class ShippingAddress < OrderAddress
  attr_accessor :has_shipping_address  
end

class OrderAddress < ActiveRecord::Base
  validates_presence_of :name
  #more validations here..
end   

#the view
<% form_for @order do |f| %>
  #...
  <% f.fields_for :shipping_address do |addr_f| %>
    <%= addr_f.check_box :has_shipping_address %>
    <%= …
Run Code Online (Sandbox Code Playgroud)

activerecord nested ruby-on-rails nested-forms

5
推荐指数
1
解决办法
5249
查看次数

在Rails中将多级嵌套表单标记为"脏"

我在Rails中有一个三级多嵌套表单.设置如下:项目有许多里程碑,里程碑有很多笔记.目标是使用JavaScript在页面中编辑所有内容,我们可以在页面中的项目中添加多个新的里程碑,并将新的Notes添加到新的和现有的里程碑.

一切都按预期工作,除了当我向现有的里程碑添加新笔记(新的里程碑在向他们添加笔记时工作正常)时,新笔记将不会保存,除非我编辑任何实际属于里程碑的字段以标记形式"脏"/编辑.

有没有办法标记里程碑,以便添加的新Notes将保存?

编辑:对不起,很难粘贴所有代码,因为有很多部分,但是这里有:

楷模

class Project < ActiveRecord::Base
  has_many :notes, :dependent => :destroy
  has_many :milestones, :dependent => :destroy

  accepts_nested_attributes_for :milestones, :allow_destroy => true
  accepts_nested_attributes_for :notes, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end

class Milestone < ActiveRecord::Base
  belongs_to :project
  has_many :notes, :dependent => :destroy

  accepts_nested_attributes_for :notes, :allow_destroy => true, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end

class Note < ActiveRecord::Base
  belongs_to :milestone
  belongs_to :project

  scope :newest, lambda { |*args| order('created_at …
Run Code Online (Sandbox Code Playgroud)

ruby forms ruby-on-rails nested-forms

5
推荐指数
1
解决办法
1610
查看次数

销毁方法不会以复杂的形式销毁嵌套资源

我正在使用Paperclip 2.3.8运行Rails 3.0.3.我有两个模型,称它们为"Post"和"Image".Image是一个包含Paperclip图像文件的对象,用于动态地将图像添加到Posts.图片属于Post,Post有很多图片.

邮政模式:

class Post < ActiveRecord::Base
 has_many :images
 accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:image].blank? },  :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

图像模型:

class Image < ActiveRecord::Base
 belongs_to :post
 has_attached_file :image,
      :styles => {
      :thumb=> "100x100#",
      :small  => "300x300>" }
end
Run Code Online (Sandbox Code Playgroud)

我有一个表单,我想动态添加和删除图像.如果图像是正在上传的新图像,我想显示file_field; 否则,如果图像已经存在,我想显示图像和删除链接.我跟着Ryan Bates在嵌套表格上的Railscasts.动态添加图像是有效的,但是对_destory的调用没有触发.帖子包含"_destroy"=>"1",但在HTTP Post事件之后,id为7的图像仍然存在.以下是HTTP Post的摘录:

"images_attributes"=>{
  "0"=>{"id"=>"7", "_destroy"=>"1"}, 
  "1"=>{"id"=>"8", "_destroy"=>"false"}, 
  "2"=>{"id"=>"9", "_destroy"=>"false"}, 
  "3"=>{"id"=>"10", "_destroy"=>"false"}}
Run Code Online (Sandbox Code Playgroud)

这是表单的样子:

<%= form_for @post, :html => { :multipart => true } do |f| %>
 <%= f.label :images, 'Images' %><br />
 <div class="field">
  <%= f.fields_for …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails paperclip nested-forms nested-attributes ruby-on-rails-3

5
推荐指数
1
解决办法
5972
查看次数

如何停止使用已添加记录复制的fields_for嵌套表单?

我有一个场地表,我在场地编辑页面上使用嵌套表格为每个场地添加优惠.但是,每次添加新商品时,fields_for表单都会保存输入的文本,并为要添加的其他商品记录创建新的空白表单.

我只想为每个添加的记录添加一个"添加新优惠"表单.

没有添加任何优惠 - 这很好: 在此输入图像描述

添加了一个优惠 - 现在theres 2'添加新优惠'表单和不需要的空白优惠部分:

在此输入图像描述

添加一个优惠后,这就是我想要的样子: 在此输入图像描述

新的空白报价表格和空白部分的数量随着控制器中内置的数字的变化而变化(在1分钟时)

场馆管制员

class VenuesController < ApplicationController   
  def edit
    @venue = Venue.find(params[:id])
    1.times { @venue.offers.build }
  end

  def update
    @venue = Venue.find(params[:id])
    if @venue.update_attributes(params[:venue])
      flash[:notice] = 'Venue updated successfully'
      redirect_to :back
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

场地模型

class Venue < ActiveRecord::Base
  has_many :offers
  accepts_nested_attributes_for :offers
end
Run Code Online (Sandbox Code Playgroud)

场地edit.html.erb

      <%= form_for @venue do |f| %>

        <h2 class="venue_show_orange">Offers</h2>

        <div class="edit_venue_details">
          <% if @venue.offers.count.zero? %>
            <div class="no_offers">
              No offers added yet.
            </div>
          <% else %>
            <%= render …
Run Code Online (Sandbox Code Playgroud)

nested ruby-on-rails build nested-forms fields-for

5
推荐指数
1
解决办法
2779
查看次数

我的.on()表现得像.bind(),而不是.live()

我有一个复杂的嵌套形式(Ryan Bates的版本).live()附加到一些动态生成的元素,我现在正在转换到.on()从Jquery 1.4升级到1.7.

以下是~22个变化之一的示例:

# old version with .live()
$('.options .image').live('click', function(){
    console.log('clicked .options')
})

# new version with .on()
$('.options').on('click', '.image', function(){
    console.log('clicked .options')
})
Run Code Online (Sandbox Code Playgroud)

这些更改对于已存在的表单元素非常有效,但它们对于之后动态创建的任何嵌套元素都失败了.因此,它的工作更像Jquery而bind不是live.你知道这里会发生什么吗?

由于代码的复杂程度太高而且很多部分内容我暂时搁置(希望你有预感!).谢谢.

jquery ruby-on-rails nested-forms

5
推荐指数
1
解决办法
139
查看次数

Capybara nested_form Poltergeist选择动作

我正在使用梦幻般的nested_form gem来处理我的嵌套属性,这些属性非常有效.

当我尝试使用Capybara/rspec与Poltergeist作为驱动程序在我的嵌套表单的选择字段上运行一些集成测试时,我的问题就出现了.

我的测试如下.

click_on "Add Field"    
find('input:last').set "Employee Code"   
find('select:last').select "Word"
Run Code Online (Sandbox Code Playgroud)

我在rspec中收到以下错误

Failure/Error: find('select:last').select "Word"
 Capybara::Poltergeist::ObsoleteNode:
   Capybara::Poltergeist::ObsoleteNode
Run Code Online (Sandbox Code Playgroud)

当我在尝试找到'select:last'之前打印html内容时,我能够看到正确的html.贴在下面.

<input class="string required" id="source_model_attributes_attributes_1355377152026_field_name" label="Field Name" name="source[model_attributes_attributes][1355377152026][field_name]" placeholder="Field Name" size="50" type="text">
<select class="select required" id="source_model_attributes_attributes_1355377152026_field_type" label="Field Type" name="source[model_attributes_attributes][1355377152026][field_type]" prompt="Select Field Type">
<option value="">Select Field Type</option>
<option value="Word">Word</option>
<option value="Number">Number</option>
<option value="True or False">True or False</option>
<option value="Date &amp; Time">Date &amp; Time</option>
<option value="Date">Date</option>
<option value="Time">Time</option></select>
Run Code Online (Sandbox Code Playgroud)

我正在使用find而不是fill_in,因为我的html是动态生成的,时间戳附加到id和name字段.

也许值得一提的是,我对Capybara的选择Action运气不佳,我试图像这样使用它.

select "Word", from: "Field Type"
Run Code Online (Sandbox Code Playgroud)

哪个失败,出现以下错误.

Failure/Error: select "Word", from: …
Run Code Online (Sandbox Code Playgroud)

select rspec nested-forms capybara poltergeist

5
推荐指数
0
解决办法
916
查看次数

使用Rails 4,强参数和Carrierwave上传文件

我正在移植一个使用Carrierwave到Rails 4的应用程序,但我遇到了强大的params问题.我有一个模特

accepts_nested_attributes_for :photos
Run Code Online (Sandbox Code Playgroud)

以下是上传图片的传递方式:

{
    # ...
    "model"=>
    {
        # ...
        "photos_attributes"=>
        {
            "1362752177921"=>
            {
                "image"=>"test.jpg",
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法弄清楚如何编写将接受的参数photos_attributes.

我试过.permit(photos_attributes: [])但它只是跳过它们,当我使用时permit!,uuid在保存之前创建的不会出现在SQL中,这是第二个问题:

photos.uuid may not be NULL: INSERT INTO "photos" ("created_at", "model_id", "image", "title", "updated_at") VALUES (?, ?, ?, ?, ?)
Run Code Online (Sandbox Code Playgroud)

这里缺乏强参数的文档,我甚至不确定如何继续.

更新 这适用于嵌套属性:

params.permit( ..., :photos_attributes => ['id', 'title', 'image', '_destroy'])
Run Code Online (Sandbox Code Playgroud)

但是看起来像Carrierwave或Nested Form应该先为Rails 4更新.它只是试图一直保存一个空图像.相同的代码(没有strong_params)在Rails 3中有效.

file-upload nested-forms carrierwave strong-parameters ruby-on-rails-4

5
推荐指数
1
解决办法
6252
查看次数

接受带有表单的多个模型

我有一个供应商名称和电子邮件的基本表单设置.我想将我的地址属性嵌套到这个表单中,但我一直得到以下错误

未允许的参数:地址

class Supplier < ActiveRecord::Base
  has_many :addresses, dependent: :destroy, as: :addressable    
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
  belongs_to  :supplier
end

class SuppliersController < ApplicationController

  def allowed_params
    params.require(:supplier).permit(:name, :email, {:address_attributes => [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id]})
  end
end
Run Code Online (Sandbox Code Playgroud)

供应商表格

%fieldset#admin-supplier-names.span-12
  %label Name
  = form.text_field :name
  %label Email
  = form.text_field :email

= form.fields_for :address do |address_fields|
  %li= address_fields.text_field :first_name, placeholder: :first_name.upcase, value: current_user.first_name
  %li= address_fields.text_field :last_name, placeholder: :last_name, value: current_user.last_name
  %li= address_fields.text_field :address1, placeholder: …
Run Code Online (Sandbox Code Playgroud)

forms ruby-on-rails nested-forms strong-parameters

5
推荐指数
1
解决办法
270
查看次数

在Rails中访问未保存的父母和孩子的祖父母

我有一个表格可以将一个Parent和多个Child对象保存在一起.在初始化Child对象期间,它需要访问Grandparent.这是模型的样子:

class Grandparent
  has_many :parents, inverse_of: :grandparent
end

class Parent
  belongs_to :grandparent, inverse_of: :parents
  has_many :children, inverse_of: :parent
  accepts_nested_attributes_for :children
end

class Child
  belongs_to :parent
  delegate :grandparent, to: :parent

  # Test code
  after_initialize do
    raise 'NoParentError' unless parent.present?
    raise 'NoGrandparentError' unless grandparent.present? # Errors here!
    puts 'All good!'
  end
end
Run Code Online (Sandbox Code Playgroud)

请记住,表单用于同时保存新父项和多个子项,但我正在尝试访问祖父项对象中的信息.我读到inverse_of选项应该已经完成​​了这个伎俩,但child.grandparent仍然nil不幸.

这是实际导致失败的控制器部分:

@parent = @grandparent.parents.build(parent_params)
# prior to saving @parent
Run Code Online (Sandbox Code Playgroud)

由于某种原因,父母不知道祖父母是谁.

更新

看起来我可以通过以下代码解决该错误:

@parent = Parent.new(parent_params.merge(grandparent: …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations nested-forms

5
推荐指数
1
解决办法
367
查看次数

React:如何在不获取警告的情况下使用子FormItem组件警告:validateDOMNesting:&lt;form&gt;不能显示为&lt;form&gt;的后代

鉴于父组件,我使用的是子组件DynamicFieldSet是一个分组FormItems。但是我收到错误:

Warning: validateDOMNesting(...): <form> cannot appear as a descendant of <form>. See CreateTopic > Form > form > ... > DynamicFieldSet > Form > form.

我试图删除<Form> </Form>子组件中的标签,但这是一个编译错误。

有没有一种方法可以禁用子窗体视图的呈现?

父组件

class CreateTopic extends React.Component {
render() {
    return (
      <div className="create-topic-container">
        <h3>Create an event</h3>
        <Form onSubmit={this.handleSubmit}>
          <FormItem>...</FormItem>
          <FormItem>...</FormItem>
          <FormItem>...</FormItem>
          <FormItem
            {...formItemLayout}
            label="Results"
            style={{ marginBottom: SPACING_FORM_ITEM }}
          >
            {getFieldDecorator('results', {
              rules: [
                {
                  required: true,
                  message: 'Results cannot be empty.',
                },
              ],
            })(<DynamicFieldSet
              form={this.props.form}
            />)}
          </FormItem> …
Run Code Online (Sandbox Code Playgroud)

forms nested-forms reactjs

5
推荐指数
2
解决办法
3270
查看次数