[英]simple_form with Nested Attributes
我想我會問一個由兩部分組成的問題; 如果這應該分成兩個問題,我道歉。
首先,我試圖將課程存儲到course
表(course_name,instructor,start_time,end_time等等)中,這些course_days
與course_days
有一個ActiveRecord關系( course_id
和day
與星期幾相對應=> 1周日,周一2,等等......) 我的最終目標是在FullCalendar中將這些作為時間表顯示,這就是為什么我認為將course
和course_days
分成兩個具有ActiveRecord關系的表是最好的。 使用Rails,這是實現這一目標的好方法嗎?
如果是這樣,其次,我使用Simple Form將以下數據添加到2個表: course
和course_days
。 我想我越來越近了。 我只是在添加course_days
時遇到一些困難(例如,如果檢查“星期一”,“星期二”和“星期四”,則會在course_days
添加3個單獨的行。
以下是我目前正在使用的內容:
#new.html.erb
<%= simple_form_for @course, html: { autocomplete: 'off' } do |f| %>
<%= f.input :title, label: 'Class Name', placeholder: 'Class Name' %>
<%= f.input :instructor, placeholder: "Instructor Name" %>
<%= f.input :instructor_email, placeholder: 'Instructor Email' %>
<%= f.input :building, placeholder: 'Building' %>
<%= f.input :room, placeholder: 'Room' %>
<%= f.input :semester do %>
<%= f.select :semester_id, @semesters.map { |s| [s.name, s.id] } %>
<% end %>
<%= f.simple_fields_for :course_days do |p| %>
<%= p.input :day, :as => :boolean %>
<% end %>
<%= f.button :submit, :class => 'btn btn-primary' %>
#app/models/course.rb
class Course < ActiveRecord::Base
has_many :course_days
accepts_nested_attributes_for :course_days
end
#app/models/course_day.rb
class CourseDay < ActiveRecord::Base
belongs_to :course
end
#app/controllers/courses_controller.rb
def new
@course = Course.new
7.times { @course.course_days.build } #For 7 days of the week??
@semesters = Semester.all()
end
def create
@course = Course.new(course_params)
if @course.save
redirect_to action: 'new'
flash[:notice] = "Course Created."
else
render :new
end
end
def course_params
params.require(:course).permit!
end
任何幫助將不勝感激!
編輯:我在上面的表格中遇到的問題:1)如何生成帶有指定屬性的7個復選框,以確定它是星期一,星期二等? 2)使用嵌套屬性,即使檢查了多個復選框,也只向數據庫提交一條記錄。
編輯2
所以,基本上,當提交該表單時,我希望數據庫表看起來像這樣:
courses: id => 14, name => Bio 101, start_time => 08:00:00, end_time 09:00:00
course_days: id => 11, course_id => 14, day => 1 (For monday)
course_days: id => 12, course_id => 14, day => 3 (For wednesday)
course_days: id => 13, course_id => 14, day => 5 (For friday)
請考慮具有另一個表,需要從數據庫中獲取該行。
因此,當您要求課程時,您需要最多7次點擊才能了解課程日期。
我建議您在課程模型中有一個days
列。 此列將以逗號分隔的值保存選定的天數。
這樣您就不需要accepts_nested_attributes_for
。 在這種情況下,您需要做的是使用before_validation
回調使用復選框值填充Course.days列。
我不確定,但是怎么樣:
7.times { |i| @course.course_days.build day: i } #For 7 days of the week??
這樣,您將擁有7個具有不同天數值的實例。 您可以使用i+1
從1開始。
我想這會發送類似的東西:
{
"course" => {
"course_days_attributes" =>{
"0" => { "day" => "1"},
"1" => { "day" => "5"},
}
}
}
試試這只是為了測試,然后你可以重構:
<%= f.simple_fields_for :course_days do |p| %>
<%= p.input :day, :as => :check_box, input_html: { value: f.object.course_days.day } %>
<% end %>
我不知道你是否可以使用p.object.day
而不是f.object.course_days.day
。 這應該是自動的。
我會嘗試的另一件事是:
<%= f.simple_fields_for :course_days, f.object.course_days do |p| %>
但實際上我無法解釋究竟發生了什么。
我不確定這一點。
首先,simple_form具有輸入集合
所以
<%= f.input :semester do %>
<%= f.select :semester_id, @semesters.map { |s| [s.name, s.id] } %>
<% end %>
你可以寫作
<%= f.input :semester, collection: @semesters %>
其次,要將天數作為布爾值,您可以執行以下操作:
<%= f.simple_fields_for :course_days do |p| %>
<%= p.input :day, collection: [['Monday',1],['Tuesday',2][so forth]], as: :check_boxes %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.