嵌套表单不保存

时间:2015-08-31 10:21:42

标签: ruby-on-rails forms ruby-on-rails-4 nested-forms cocoon-gem

我使用cocoon gem创建嵌套表单。我有一个有许多门票和组织者的活动模型。

活动模特:

class Event < ActiveRecord::Base
  has_many :organizers, dependent: :destroy
  has_many :tickets, dependent: :destroy

  accepts_nested_attributes_for :tickets, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :organizers, reject_if: :all_blank, allow_destroy: true

end

组织者模型:

class Organizer < ActiveRecord::Base
  belongs_to :user
  belongs_to :event

  validates :user, presence: true
  validates :event, presence: true
end

门票模特:

class Ticket < ActiveRecord::Base
  belongs_to :event
  has_many :bookings
end

EVENT CONTROLLER:

class EventsController < ApplicationController
  def new
    @event = Event.new
  end

  def create 
    @event = Event.new(event_params)
    if @event.save
      redirect_to event_path(@event)
    else
      render 'new'
    end 
  end

  def edit
    @event = Event.friendly.find(params[:id])
  end

  def update
    @event = Event.friendly.find(params[:id])
    @event.update_attributes event_params
    if @event.save
      redirect_to event_path(@event), flash: { success: "Event updated succesfully." }
    else
      render 'edit'
    end 
  end

  private
  def event_params
    params.require(:event).permit(:title,:description, tickets_attributes: [:id, :event_id, :title, :limit, :_destroy], organizers_attributes: [:id, :event_id, :user_id, :_destroy])
  end
end

新视图

<%= simple_form_for @event, url: events_path do |f| %>
  <%= f.input :title %>
  <%= f.input :description %> 
  <div id="tickets">
    <%= f.simple_fields_for :tickets do |ticket| %>
      <%= render 'ticket_fields', f: ticket %>
    <% end %>
    <div class="links">
      <%= link_to_add_association 'add ticket', f, :tickets %>
    </div>
  </div>
  <div id="organizers">
    <%= f.simple_fields_for :organizers do |organizer| %>
      <%= render 'organizer_fields', f: organizer %>
    <% end %>
    <div class="links">
      <%= link_to_add_association 'add organizer', f, :organizers %>
    </div>
  </div>

  <%= f.button :submit %>
<% end %>

组织者领域部分:

<div class="nested-fields">
  <%= f.input :user, collection: User.all, label_method: lambda { |user| user.email } %>
  <%= link_to_remove_association "remove organizer", f %>
</div>

TICKETS FIELD PARTIAL:

<div class="nested-fields">
  <%= f.input :title %>
  <%= f.input :limit, disabled: f.object.no_limit %>
  <%= f.input :no_limit %>
  <%= link_to_remove_association "remove ticket", f %>
</div>

服务器日志:

Parameters: 
  {
    "utf8"=>"✓", 
    "authenticity_token"=>"rct6..", 
    "event"=>{
      "title"=>"zdtfbvc", 
      "description"=>"fdxcv", 
      "tickets_attributes"=>{
        "1441016179377"=>{
          "title"=>"tdhfgb", 
          "limit"=>"2", 
          "_destroy"=>"false"
        }
      }, 
      "organizers_attributes"=>{
        "1441017842699"=>{
          "user"=>"3", 
          "_destroy"=>"false"
        }, 
        "1441017845556"=>{
          "user"=>"2", 
          "_destroy"=>"false"
        }
      }
    }, 
    "commit"=>"Create Event"
  }

问题是te故障单已创建并保存到数据库,但组织者没有。

1 个答案:

答案 0 :(得分:0)

问题可能是组织者模型验证了事件的存在,而事件尚未创建。

如果您想在这种情况下进行验证,则应添加inverse_of,如下所示:

class Organizer < ActiveRecord::Base
  belongs_to :user
  belongs_to :event, inverse_of: :organizers

  validates :user, presence: true
  validates :event, presence: true
end

class Event < ActiveRecord::Base
  has_many :organizers, dependent: :destroy, inverse_of: :event
  has_many :tickets, dependent: :destroy

  accepts_nested_attributes_for :tickets, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :organizers, reject_if: :all_blank, allow_destroy: true
end

请参阅:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#module-ActiveRecord::NestedAttributes::ClassMethods-label-Validating+the+presence+of+a+parent+model