使用rails从一个表单保存到多个模型

时间:2012-02-18 01:14:00

标签: ruby-on-rails ruby-on-rails-3 activerecord model-associations

在过去的几个星期里,我一直在进入轨道,我遇到了一个我无法找到答案的问题。我觉得这可能是非常简单的事情,但它让我发疯,所以我希望得到一点指导......

我将1个表单中的数据插入到2个模型中。一个模型称为Venue,一个模型称为事件。它是一对多的协会,其中多个活动属于场地,场地有许多活动。

模型:

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

class Event < ActiveRecord::Base
  belongs_to :venue

  scope :upcoming, where('date >= ?', Date.today)
 end  

控制器:

class EventsController < ApplicationController

def new
      @venue = Venue.new
end

def create

@venue = Venue.new(params[:venue])

    if @venue.save
        render :inline => "Success"
    else
        render('new')
    end
end

end

形式:

<%= form_for(@venue, :url =>{:action => 'create'}) do |f| %>

        Artist ID<br />
        <%= fields_for :event do |event_fields| %>
            <%= event_fields.text_field :artist_id %><br/><br />
        <% end %>

        Venue City<br />
        <%= f.text_field(:city) %> <br /><br />
        Venue Name<br />
        <%= f.text_field(:name) %><br/><br/>


        <div class="actions">
            <%= submit_tag "Save", :class => "btn primary" %>
        </div>

<% end %>

日志输出:

Started POST "/events" for 127.0.0.1 at 2012-02-17 19:57:24 -0500
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"p3llx5KsYn6gyjP9g2qwzXr+0rjh3h/o34h/iqvqjRo=", "event"=>{"artist_id"=>"124"}, "venue"=>{"city"=>" Boston", "name"=>"Bostons Fa'v"}, "commit"=>"Save"}
(0.2ms)  BEGIN
SQL (0.8ms)  INSERT INTO "venues" ("address_1", "address_2", "capacity", "city", "country", "created_at", "created_by", "name", "state", "updated_at", "url", "zip") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["address_1", nil], ["address_2", nil], ["capacity", nil], ["city", " Boston"], ["country", nil], ["created_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["created_by", nil], ["name", "Bostons Fa'v"], ["state", nil], ["updated_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["url", nil], ["zip", nil]]
(0.4ms)  COMMIT
Rendered inline template (0.2ms)
Completed 200 OK in 5ms (Views: 0.6ms | ActiveRecord: 1.4ms)

正如您所看到的那样,它没有失败,但它只提交给1模型。我的理解是通过添加&#34; accepts_nested_attributes_for:events&#34; Venue模型知道然后转到我的Events表,用我提供的事件数据创建一个新事件(在这个例子中只是artist_id),然后自动将我的场地表中的id字段插入venue_id。

我在这里思考的越多,我认为我可能会越少迈出一步......如果有人有一秒钟我会很感激你的思考过程。

谢谢

2 个答案:

答案 0 :(得分:1)

我认为表单中存在语法错误。 fields_for是f的方法,它需要一个复数符号。以单数形式使用块的迭代器也是一种很好的做法,当你的方法只需要一个参数时,不要使用括号:

<%= form_for(@venue, :url =>{:action => 'create'}) do |f| %>

        Artist ID<br />
        <%= f.fields_for :events do |event_field| %>
            <%= event_field.text_field :artist_id %><br/><br />
        <% end %>

        Venue City<br />
        <%= f.text_field :city %> <br /><br />
        Venue Name<br />
        <%= f.text_field :name %><br/><br/>


        <div class="actions">
            <%= submit_tag "Save", :class => "btn primary" %>
        </div>

<% end %>

请参阅Ryan Bate's Railscasts中嵌套表单的示例:

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <% f.fields_for :questions do |builder| %>
  <p>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows => 3 %>
  </p>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>

答案 1 :(得分:0)

Venue模型需要指定哪些属性是可批量分配的,包括Event属性。

attr_accessible :event_attributes, :address_1, :address_2, :capacity # ...