嵌套形式参数空白?(nested form params blank? problems)

编程入门 行业动态 更新时间:2024-10-27 12:25:59
嵌套形式参数空白?(nested form params blank? problems)

亲爱的程序员,

我在另一个表单中的嵌套表单有点问题。这是我正在尝试做的,这个当前表单是将所有者添加到current_customer。 但是在这种形式中有一个嵌套的形式,用于将所有者的电话号码输入到电话表中,该电话表与所有者表分开 - 因此是嵌套表格。 用户必须输入其所有者名称才能创建新所有者,但稍后可以添加电话号码。 上述表格如下:

<%= form_for(@owner) do |f| %> <%= f.hidden_field :customer_id, :value => params[:id] %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.fields_for :telephones do |t| %> <div class="form-group"> <%= t.hidden_field :owner_id, :value => @owner.id %> <div class="number-type"> <%= t.label :tel, "Contact number" %> <%= t.radio_button :numbertype, "mobile", :checked => false, :class => "numbertype mobile" %> <%= t.label :numbertype, "Mobile", :value => "mobile" %> <%= t.radio_button :numbertype, "land", :class => "numbertype land" %> <%= t.label :numbertype, "Land", :value => "land" %> </div> <%= t.text_field :number, placeholder: "telephone number", class: 'form-control some_input' %> <%= t.text_field :ext, placeholder: "extension", id: "extension", class: 'form-control'%> </div> <% end %> <%= f.submit "Add new owner", class: "btn btn-primary" %>

telephone.rb模型如下:

class Telephone < ActiveRecord::Base belongs_to :customer belongs_to :owner validates :owner_id, presence: true, allow_blank: true validates :customer_id, presence: true, allow_blank: true validates :numbertype, presence: true, allow_blank: true validates :number, presence: true, allow_blank: true, length: { in: 7..10 }, uniqueness: { scope: [:ext] }, :numericality => {:only_integer => true} validates :ext, presence: true, allow_blank: true end

owners_controller.rb

def create @customer = Customer.find(params[:owner][:customer_id]) @owner = Owner.find_by(customer_id: params[:id]) if number_blank? @owner = @customer.owners.build(owner_params) else @owner = @customer.owners.build(owner_with_telephone_params) end if @owner.save @customer.update_attribute(:updated_at, Time.zone.now) flash[:success] = "New owner added!" redirect_to request.referrer else flash[:danger] = "no owner has been added." redirect_to @customer end end private def owner_with_telephone_params params.require(:owner).permit(:name, :email, :tel, telephones_attributes: [:numbertype, :number, :ext]) end def owner_params params.require(:owner).permit(:name, :email, :tel) end def number_blank? params[:owner][:telephones_attributes][:number].blank? end

我面对我的代码的问题是,即使我的:此表单中的数字字段已填写,该号码也无法输入电话表。 是否好像导轨无法检测到我的:数字字段是空白的? 或不。 如果有什么我做错了? 非常感谢你的帮助!

dear fellow programmers,

I am having a little problem with my nested form within another form.. here is what i'm trying to do, this current form is to add owners to current_customer. But within this form has a nested form to input owner's telephone number into the telephones table which is separated from the owners table -- hence the nested form. Users have to enter their owners name to create a new owner, but telephone number can be added later. The said form is as follows:

<%= form_for(@owner) do |f| %> <%= f.hidden_field :customer_id, :value => params[:id] %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.fields_for :telephones do |t| %> <div class="form-group"> <%= t.hidden_field :owner_id, :value => @owner.id %> <div class="number-type"> <%= t.label :tel, "Contact number" %> <%= t.radio_button :numbertype, "mobile", :checked => false, :class => "numbertype mobile" %> <%= t.label :numbertype, "Mobile", :value => "mobile" %> <%= t.radio_button :numbertype, "land", :class => "numbertype land" %> <%= t.label :numbertype, "Land", :value => "land" %> </div> <%= t.text_field :number, placeholder: "telephone number", class: 'form-control some_input' %> <%= t.text_field :ext, placeholder: "extension", id: "extension", class: 'form-control'%> </div> <% end %> <%= f.submit "Add new owner", class: "btn btn-primary" %>

The telephone.rb model is as follows:

class Telephone < ActiveRecord::Base belongs_to :customer belongs_to :owner validates :owner_id, presence: true, allow_blank: true validates :customer_id, presence: true, allow_blank: true validates :numbertype, presence: true, allow_blank: true validates :number, presence: true, allow_blank: true, length: { in: 7..10 }, uniqueness: { scope: [:ext] }, :numericality => {:only_integer => true} validates :ext, presence: true, allow_blank: true end

owners_controller.rb

def create @customer = Customer.find(params[:owner][:customer_id]) @owner = Owner.find_by(customer_id: params[:id]) if number_blank? @owner = @customer.owners.build(owner_params) else @owner = @customer.owners.build(owner_with_telephone_params) end if @owner.save @customer.update_attribute(:updated_at, Time.zone.now) flash[:success] = "New owner added!" redirect_to request.referrer else flash[:danger] = "no owner has been added." redirect_to @customer end end private def owner_with_telephone_params params.require(:owner).permit(:name, :email, :tel, telephones_attributes: [:numbertype, :number, :ext]) end def owner_params params.require(:owner).permit(:name, :email, :tel) end def number_blank? params[:owner][:telephones_attributes][:number].blank? end

the problem i am facing with my code is that even my :number field in this form is filled, the number cannot be entered into the telephone table. Is it as if rails could not detect my :number field is blank? or not. if there something that i am doing wrong? much appreciate your help!

最满意答案

使用嵌套表单时, telephone_attributes的嵌套关系返回哈希散列,其中每个子哈希可以表示不同的电话记录。 以下是将params提交到您的应用程序时的样子(假设提供了1部电话):

{ "owner" => { "telephones_attributes" => { "0" => { "number" => "555-1234" } } }

那么在这种情况下你对number_blank?的调用number_blank? 将永远为true因为没有:number数值作为:telephones_attributes哈希的直接键。 相反,您需要深入了解该哈希的值,以查看是否已设置任何数字。 这样的东西可能有效(未经测试):

def number_blank? params[:owner][:telephones_attributes].values.map { |telephone| telephone[:number] }.reject(&:blank?).empty? end

这将从嵌套字段构建所有提供的数字的新数组,删除所有空白元素,并检查数组是否为空。 如果它是空的,则没有数字,否则至少提供一个数字。

然而,对于Rails可能已经为您解决的问题,这对我来说是一项很大的工作。 您是否考虑过对Owner模型上的accepts_nested_attributes_for调用使用:reject_if选项? 您可以使用它来拒绝任何数字为空的参数,因此不会在电话表中创建记录。 这有助于消除控制器#create action中的条件逻辑。 查看ActiveRecord :: NestedAttributes的文档。

When using nested forms, your nested relationship of telephone_attributes returns a hash of hashes, where each child hash can represent a different telephone record. Here's what the params look like when they are submitted to your application (assuming 1 telephone was provided):

{ "owner" => { "telephones_attributes" => { "0" => { "number" => "555-1234" } } }

So in this case your call to number_blank? will always be true because there is no :number value as a direct key to the :telephones_attributes hash. Instead you need to dive into the values of that hash to see if any numbers have been set. Something like this might work (untested):

def number_blank? params[:owner][:telephones_attributes].values.map { |telephone| telephone[:number] }.reject(&:blank?).empty? end

This builds a new array of all of the provided numbers from the nested fields, removes any blank elements, and checks if the array is empty. If it's empty, there are no numbers, otherwise there was at least one number provided.

However this strikes me as a lot of work for a problem that Rails might have already solved for you. Have you looked into using the :reject_if option for the accepts_nested_attributes_for call on your Owner model? You might be able to use that to reject any parameters where the number is blank so no record is created in the telephones table. This would help eliminate the conditional logic out of your controller #create action. Check out the documentation for ActiveRecord::NestedAttributes.

更多推荐

本文发布于:2023-07-09 14:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   空白   形式   参数   nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!