嵌套的属性表格验证错误

时间:2016-10-11 23:05:30

标签: ruby-on-rails debugging routing

我看到一个嵌套属性表单的奇怪错误。我有两个模型,派对嘉宾。派对has_many宾客,宾客are_to党。我有一个通过Party创建嘉宾的表单。

我看到的错误如下:

  • 当我第一次访问表单页面时,所有内容都加载但我无法提交数据。如果我在呈现页面时立即刷新页面,那么当我按下提交时,无论我输入什么内容都会打到数据库。我发现当你点击它时,提交按钮会变为活动状态,但是当它不起作用时它永远不会离开它。

为什么我必须在表单提交数据之前刷新页面?我很感激帮助解开这个问题。我甚至不确定哪个线程开始拉扯。

party.rb

class Party < ApplicationRecord

  belongs_to :event
  has_many :guests, inverse_of: :party
  accepts_nested_attributes_for :guests, :reject_if => proc { |attribute| attribute[:last_name].blank? }
  validate :validate_nested_attributes

  def validate_nested_attributes
    guests.each do |guest|
      errors.add(:first_name, "first name cannot be blank") if guest.first_name.blank?
      errors.add(:email, "must be valid") if !email_is_valid(guest.email)
    end
  end

  def email_is_valid(email)
    email =~ /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  end

end

guest.rb

class Guest < ApplicationRecord

  belongs_to :party
  belongs_to :event


end

parties_controller.rb

class PartiesController < ApplicationController

  def index
  end  

  def new
    @event = Event.friendly.find(params[:event_id])
    @party = @event.parties.new
    10.times {@party.guests.build}
  end

  def create
    @event = Event.friendly.find(params[:event_id])
    @party = @event.parties.new(party_params)
    if @party.save
      redirect_to event_guests_path(@event)
    else
      render 'new'
    end
  end


  private

    def party_params
      params.require(:party).permit(:id, :party_name, guests_attributes: [:event_id, :id, :first_name, :last_name, :email, :phone])
    end
end

new.html.erb

<div class="col-sm-10 col-sm-offset-1"><h2>Create a New Invitation Party</h2></div>

  <div class="form-group col-sm-10 col-sm-offset-1 well" id="guests">
    <ul>
      <% if @party.errors.any? %>
          <% @party.errors.each do |attribute, msg| %>
            <li><%= "#{attribute} #{msg}" if @party.errors[attribute].first == msg %>
          <% end %>
        <% end %>
      <%= form_for [@event, @party] do |f| %>
    </ul>

      <%= render :partial => 'guest_fields', :locals => { :f => f } %>
  </div>

  <div class="col-sm-8 col-sm-offset-2 pull-right">
    <%= f.submit "Create Party", class: "btn btn-primary pull-right", style: "margin-right: 10%;" %>
    <%= link_to "Back", event_guests_path(@event), class: "btn btn-danger pull-right", style: "margin-right: 5px;" %> 
  </div>


<% end %>

_guest_fields.html.erb

<%= f.fields_for :guests do |ff| %>

  <%= ff.hidden_field :event_id, :value => @event.id %>

  <div class="row">
    <div class="form-group col-sm-3">
      <%= ff.text_field :first_name, placeholder: "First Name", class: "form-control" %>
    </div>

    <div class="form-group col-sm-3">
      <%= ff.text_field :last_name, placeholder: "Last Name", class: "form-control" %>
    </div>

    <div class="form-group col-sm-3">
      <%= ff.text_field :email, placeholder: "Email Address", class: "form-control" %>
    </div>

    <div class="form-group col-sm-3">
      <%= ff.text_field :phone, placeholder: "Phone Number", class: "form-control" %>
    </div>
    <hr>
  </div>
<% end %>

有关GET请求的Rails服务器日志

Started GET "/events/2993Alexandro/parties/new" for -.-.-.- at 2016-10-11 23:00:56 +0000
Cannot render console from -.-.-.-! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PartiesController#new as HTML
  Parameters: {"event_id"=>"2993Alexandro"}
  Event Load (0.5ms)  SELECT  "events".* FROM "events" WHERE "events"."path" = $1 ORDER BY "events"."id" ASC LIMIT $2  [["path", "2993Alexandro"], ["LIMIT", 1]]
  Rendering parties/new.html.erb within layouts/application
  Rendered parties/_guest_fields.html.erb (9.7ms)
  Rendered parties/new.html.erb within layouts/application (15.9ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Rendered layouts/_left_bar.html.erb (1.4ms)
Completed 200 OK in 180ms (Views: 98.5ms | ActiveRecord: 0.8ms)

2 个答案:

答案 0 :(得分:0)

  <%= form_for [@event, @party] do |f| %> 
</ul> 

为什么您的开放式广告代码在ul内?匹配结束在哪里?表单是一个块级组件 - 它应该完全包含在外部块中 - 不是一个标签的一半而另一个标签的一半。

我不知道这是否能解决您的问题,但它肯定会修复您的HTML。尝试更像这样的事情:

<div class="col-sm-10 col-sm-offset-1"><h2>Create a New Invitation Party</h2></div>

<div class="form-group col-sm-10 col-sm-offset-1 well" id="guests">
  <ul>
    <% if @party.errors.any? %>
        <% @party.errors.each do |attribute, msg| %>
          <li><%= "#{attribute} #{msg}" if @party.errors[attribute].first == msg %>
        <% end %>
    <% end %>
  </ul>

  <%= form_for [@event, @party] do |f| %>

    <%= render :partial => 'guest_fields', :locals => { :f => f } %>


    <div class="col-sm-8 col-sm-offset-2 pull-right">
      <%= f.submit "Create Party", class: "btn btn-primary pull-right", style: "margin-right: 10%;" %>
      <%= link_to "Back", event_guests_path(@event), class: "btn btn-danger pull-right", style: "margin-right: 5px;" %> 
    </div>

  <% end %>
</div>

答案 1 :(得分:0)

尝试这个我希望我能帮忙

def new
@event = Event.friendly.find(params[:event_id])
@party = @event.parties.new(party_params)
10.times {@party.guests.build}
end