标签: nested-forms

Rails嵌套对象表单*_attributes

我正在使用Rails 2.3.2,并试图让嵌套的对象表单正常工作.我已经将我的问题缩小到Rails未设置我的嵌套表单元素以及启动accepts_nested_attributes_for处理所需的*_attributes的问题.

我的型号代码是:

class Person < Party
  has_one :name, :class_name => "PersonName"
  accepts_nested_attributes_for :name, :allow_destroy => true
end

class PersonName < ActiveRecord::Base
  belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)

我的视图代码看起来像这样(我正在使用HAML):

%h3 New customer
= error_messages_for :person, :person_name, :name, :country

- form_for :person, :url => collection_url, :html => {:class => 'MainForm'} do |person_form|

  - @person.build_name unless @person.name
  - person_form.fields_for :name do |name_form|
    = name_form.label :given_name, "First Name:"
    = name_form.text_field :given_name

    = name_form.label :family_name, "Last Name:"
    = name_form.text_field :family_name

  = hidden_field_tag :inviter_id, params[:inviter_id]
  = hidden_field_tag :inviter_code, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms

6
推荐指数
1
解决办法
6071
查看次数

在Rails中使用accepts_nested_attributes_for +质量分配保护

假设你有这个结构:

class House < ActiveRecord::Base
  has_many :rooms
  accepts_nested_attributes_for :rooms
  attr_accessible :rooms_attributes
end

class Room < ActiveRecord::Base 
  has_one :tv
  accepts_nested_attributes_for :tv
  attr_accessible :tv_attributes
end

class Tv 
  belongs_to :user
  attr_accessible :manufacturer
  validates_presence_of :user
end
Run Code Online (Sandbox Code Playgroud)

请注意,Tv的用户无法故意访问.所以你有一个三层嵌套的表格,允许你在一个页面上输入房子,房间和电视.

这是控制器的创建方法:

def create
  @house = House.new(params[:house])

  if @house.save
    # ... standard stuff
  else
    # ... standard stuff
  end
end
Run Code Online (Sandbox Code Playgroud)

问题:你将如何填充user_id每个电视(它应该来自current_user.id)?什么是好的做法?

这是我在这看到的catch22.

  1. user_ids直接填充params哈希(它们非常嵌套)
    • 保存将失败,因为user_ids不可批量分配
  2. 在#save完成后为每个电视填充用户
    • 保存将失败,因为user_id必须存在
    • 即使我们绕过上述情况,电视也会暂时没有ids - 很糟糕

有任何体面的方式吗?

ruby ruby-on-rails nested-forms mass-assignment

6
推荐指数
1
解决办法
4515
查看次数

nested_form gem出错:参数个数错误(4个为3)

我已经在这个问题上苦苦挣扎了几天,似乎无法弄清楚出了什么问题.我试图允许多态文件附件到Item属于模型的模型Location.我的路线定义为:

resources :locations do
  resources :items
    post :sort
end

resources :items do
  resources :assets #model for attachments
end
Run Code Online (Sandbox Code Playgroud)

我遵循了一个关于使用carrierwave和nested_form完成此操作的教程.但是,在设置完所有内容后,在NewItem模型请求操作时出现以下错误:wrong number of arguments (4 for 3).它告诉我在该视图的第7行发生了错误:

<%= nested_form_for [@location, @item], :html => { :multipart => true } do |f| %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <%= f.fields_for :assets do |a_form|  %> ### LINE 7 ####
    <p>
      <%= a_form.label :file …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms carrierwave ruby-on-rails-3.1

6
推荐指数
1
解决办法
1137
查看次数

动态嵌套表单link_to_add调用两次

我正在使用ryan bates nested_form gem来动态地向表单添加一些嵌套字段.

例如

<%= f.fields_for :phones do |phone_form| %>
<%= phone_form.text_field :phone_number %>
<% end %>
<%= f.link_to_add "Add a phone", :phones %></p>
Run Code Online (Sandbox Code Playgroud)

一切正常,但每次点击链接时都会添加两个空字段.

我放了一个断点 $('form a.add_nested_fields').live('click', function() ,看到它被叫了两次......

我在mac上使用chrome

javascript jquery ruby-on-rails nested-forms nested-form-for

6
推荐指数
1
解决办法
2290
查看次数

使用simple_form,nested_form和twitter-bootstrap嵌套表格式

更新: 我在做了一些挖掘并意识到这可能是twitter-bootstrap导致问题后更新了这个.

这是我的嵌套表单的粗略版本:

<%= simple_nested_form_for @user, :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <%= f.input :email %>
    <%= f.input :name_first %>
    <%= f.input :name_last %>

            <table class="table table-striped">
              <thead>
                <tr>
                  <th>Active</th>
                  <th>Company</th>
                  <th>Role</th>
                  <th>Actions</th>
                </tr>
              </thead>
              <tbody>
                  <%= f.simple_fields_for :roles, :wrapper_tag => :tr do |role_form| %>
                    <td><%= role_form.hidden_field :id %><%= role_form.input :active, :label => false, :wrapper => false %></td>
                    <td><%= role_form.association :company, :label => false, :wrapper => false %></td>
                    <td><%= role_form.input :role, :label => false, :collection …
Run Code Online (Sandbox Code Playgroud)

nested-forms ruby-on-rails-3 simple-form twitter-bootstrap

6
推荐指数
1
解决办法
5637
查看次数

无法批量分配受保护的属性:配置文件,

我阅读了很多相关的帖子,但找不到为什么它不适合我.我还有一个"无法大量分配受保护的属性:个人资料"......我做错了什么?

我有一个User模型和一个具有一对一关系的相关Profile模型.这里的用户模型(简化)

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :profile_attributes, :profile_id
  has_secure_password

  has_one :profile

  accepts_nested_attributes_for :profile
end
Run Code Online (Sandbox Code Playgroud)

Profile模型

class Profile < ActiveRecord::Base
attr_accessible :bio, :dob, :firstname, :gender, :lastname, :user_id

belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我的用户控制器

def new
@user = User.new 
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @user }
  end
end

def create
@user = User.new(params[:user])
@user.build_profile

respond_to do |format|
  if @user.save

    format.html { redirect_to @user, flash: {success: 'User was successfully created. Welcome !'} }
    format.json …
Run Code Online (Sandbox Code Playgroud)

nested-forms mass-assignment nested-attributes ruby-on-rails-3.2

6
推荐指数
1
解决办法
603
查看次数

自定义模型验证错误消息警报

我正在尝试自定义用户在错误输入数据时在窗体顶部看到的错误消息警报.我正在尝试自定义的错误消息提醒是针对嵌套形式的模型属性.

我在这里尝试过编写文件的解决方案,config/locales/en.yml但这只会更改消息而不是错误消息之前显示的模型和属性的名称.

我也尝试过Billy在他的回答中提出的建议,结果相同.即

1个错误禁止保存这个徒步旅行车:
- 来自"我的自定义空白错误消息"的路线指示

有没有办法让我在错误消息中显示更加用户友好的模型和属性名称,或者从错误消息中完全删除它们?

这是我有的:

配置/语言环境/ en.yml

    # Sample localization file for English. Add more files in this directory for other locales.
    # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:  
  activerecord:
    models: 
      direction: "In the Getting There section"
    attributes:
      direction:
        directions_from: "From field"
    errors:
      full_messages:
      format: "%{message}"
      models:
        direction:
          attributes:
            directions_from:
              blank: "My Custom Blank Error Message"
Run Code Online (Sandbox Code Playgroud)

模型

class Direction < ActiveRecord::Base
  belongs_to :hikingtrail

  attr_accessible :directions_by, :directions_desc, :directions_from

  validates :directions_from, …
Run Code Online (Sandbox Code Playgroud)

alert locale nested-forms nested-attributes ruby-on-rails-3

6
推荐指数
1
解决办法
5204
查看次数

Rails在保存之前验证孩子

我有一个通常的嵌套模型

class Parent
  has_one :child
  accepts_nested_attributes_for :child
end

class Child
  belongs_to :parent

  validate :name, :presence => true
end
Run Code Online (Sandbox Code Playgroud)

如果我试图保存一个没有名字的孩子,那么它是禁止的,但如果我保存了嵌套孩子的父母,如果忽略了验证.

我不想重复我的孩子验证:reject_if.

我如何验证孩子,并且只有在孩子有效的情况下,将父母与孩子一起保存?

ruby-on-rails nested-forms ruby-on-rails-4

6
推荐指数
1
解决办法
3979
查看次数

ActiveAdmin嵌套表单多选

我正在尝试通过产品表单更新product_suppliers.该表单显示供应商表中的所有供应商,但不更新连接表.不确定错误在哪里.索引和节目显示正确的详细信息,但编辑不更新连接表.开始在这个圈子四处走动.

更新:将表格更改为下面让我接近.但仍然没有更新连接表.但是,如果我手动将行添加到连接表,则删除按预期工作.它们显示并可以删除.保存会将新的product_id添加到行中,而不是关联的supply_company_id值.我认为它是一个属性问题,但我无法看到它.

应用程序/模型/ product.rb

 class Product < ActiveRecord::Base
   ### shortned for clarity
  has_many :product_suppliers, :foreign_key => 'product_id'
  has_many :supply_companies, :through => :product_suppliers
  accepts_nested_attributes_for :product_suppliers, :allow_destroy => true
 end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ supply_company.rb

 class SupplyCompany < ActiveRecord::Base
  has_many :products, :through => :product_suppliers
  has_many :product_suppliers, :foreign_key => 'supply_company_id'
 end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ product_supplier.rb

class ProductSupplier < ActiveRecord::Base
 belongs_to :product
 belongs_to :supply_company
 accepts_nested_attributes_for :product
 accepts_nested_attributes_for :supply_company
end
Run Code Online (Sandbox Code Playgroud)

/app/admin/product.rb

ActiveAdmin.register Product do

  # See permitted parameters documentation:
  # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
  #
   permit_params :id, :product_name, :product_description, :product_type_id, :product_category_id, :product_colour_id, …
Run Code Online (Sandbox Code Playgroud)

nested-forms activeadmin ruby-on-rails-4

6
推荐指数
1
解决办法
3060
查看次数

Rails - 如何避免在视图中使用hidden_​​fields将值传递给控制器​​?

有没有办法可以避免hidden_field将视图中的值传递给控制器​​的方法?出于安全原因,我更喜欢控制器方法.不幸的@variables是,不支持价值配对strong_parameters.

编辑6/18美国东部时间下午1:00

  1. 我已将garages控制器重命名为appointments
  2. cars_controller不再创造新的appointment(正式的garages).在该中创建新约会 appointments_controller

我目前的结构


路线

Rails.application.routes.draw做

resources :techs, only: [:index, :show], shallow: true do
    resources :cars, only: [:new, :create]
end
Run Code Online (Sandbox Code Playgroud)

资源:约会

#For searchkick
resources :cars, only: [:show] do
    collection do
        get 'search'
    end
end

root "home#index"

end
Run Code Online (Sandbox Code Playgroud)

楷模

tech.rb

class Tech < ActiveRecord::Base
    searchkick

    has_many :appointments
    has_many :customers, :through => :appointments
    has_many :service_menus
    has_many :services
    has_many :cars
end
Run Code Online (Sandbox Code Playgroud)

service.rb

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

ruby ruby-on-rails nested-forms ruby-on-rails-4

6
推荐指数
1
解决办法
458
查看次数