1
나는 레일을 처음 사용하며 사용자가 이미 장소와 조직에서 장소와 조직을 선택할 수있는 새로운 이벤트를 만들려고합니다. 데이터베이스에 존재합니다. 연결을 통해 has_many를 사용하고 있습니다. 이제 place_id 또는 organization_id 필드없이 이벤트를 저장하려고 할 때도 이벤트를 저장할 수 없습니다.레일 4 : 연관을 통해 has_many를 사용하여 새로운 형식으로 모델을 저장하는 방법
모델 :
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 %>
내가 바람직 조직, 저장하는 이벤트를 얻기 위해 여기 실종 오전 organization_id 및 place_id 값을 갖는 장소 필드?
['fields_for'] (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for)를 살펴보십시오. – mdesantis