【问题标题】:Rails 4: How to to save models in new form with has_many through associationRails 4:如何通过关联使用 has_many 以新形式保存模型
【发布时间】:2014-05-14 19:56:28
【问题描述】:

我是 Rails 新手,我正在尝试创建一个新事件,允许用户从数据库中已存在的地点和组织中选择地点和组织。我正在通过关联使用 has_many。现在我无法保存活动,即使我尝试在没有 place_id 或 organization_id 字段的情况下保存活动。

型号:

class Event < ActiveRecord::Base
  belongs_to :organization
  belongs_to :place
end

class Organization < ActiveRecord::Base
  has_many :events
  has_many :places, through: :events
end

class Place < ActiveRecord::Base
  has_many :events
  has_many :organizations, through: :events
end

事件控制器:

 def new
    @event = Event.new
  end

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

def event_params
    params.require(:event).permit(:name, :description, :contact, :tag_list, :address, :latitude, :longitude, :long_description, :event_date, :start_time, :end_time, :organization_id, :place_id)
  end

新事件视图:

  <%= form_for @event do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.label :event_date %>
  <%= f.text_field :event_date %>
  <%= f.label :start_time %>
  <%= f.text_field :start_time %>
  <%= f.label :end_time %>
  <%= f.text_field :end_time %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.label :contact %>
  <%= f.text_field :contact %>
  <%= f.label :address %>
  <%= f.text_field :address %>
  <%= f.label :long_description %>
  <%= f.text_field :long_description %>
  <%= f.label :tag_list, "Tags (separated by commas)" %>
  <%= f.text_field :tag_list %>
  <%= f.submit "Save event", class: "button" %>
<% end %>

我在这里缺少什么来保存事件,最好是组织和地点字段具有组织 ID 和地点 ID 的值?

【问题讨论】:

标签: ruby ruby-on-rails-4 has-many-through model-associations


【解决方案1】:

你应该看看 accepts_nested_attributes_forfields_for。然后把你的模型改成这样

class Organization < ActiveRecord::Base
  has_many :events
  has_many :places, through: :events
  accepts_nested_attributes_for :places
end

class Place < ActiveRecord::Base
  has_many :events
  has_many :organizations, through: :events
  accepts_nested_attributes_for :organizations
end

这样你的 event_params 会是这样的

def event_params

params.require(:event).permit(:name, :description, :contact, :tag_list, :address, :latitude, :longitude, :long_description, :event_date, :start_time, :end_time, places_attributes: [:some_attribute_of_place,..], organizations_attributes: [:some_attribute_of_organization,..)

end

你的表格可以写成,

<%= form_for @event do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.label :event_date %>
  <%= f.text_field :event_date %>
  <%= f.label :start_time %>
  <%= f.text_field :start_time %>
  <%= f.label :end_time %>
  <%= f.text_field :end_time %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.label :contact %>
  <%= f.text_field :contact %>
  <%= f.label :address %>
  <%= f.text_field :address %>
  <%= f.label :long_description %>
  <%= f.text_field :long_description %>
  <%= f.label :tag_list, "Tags (separated by commas)" %>
  <%= f.text_field :tag_list %>

  <%= f.fields_for :places do |p| %>

  ---- your code-----

  <% end %>

  <%= f.fields_for :organizations do |o| %>

  ------ your code-----

  <% end %>

  <%= f.submit "Save event", class: "button" %>

<% end %>

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多