0
我不能拯救學生一個POST相處時,我「米項目節約從控制器前往相關模型
我的項目控制器看起來像:
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def new
@project = Project.new
3.times do
student = @project.students.build
end
end
def create
@project = Project.new(project_params)
@project.status = "Waiting"
# I'm not sure about these lines
@project.students.each do |student|
student = Student.new(params[:name])
end
@project.save!
redirect_to projects_path
end
private
def project_params
params.require(:project).permit(:name, :lecturer)
end
end
而一個new_project
視圖長相喜歡:
<h1>Creating new project...</h1>
<%= form_for @project, url: projects_path do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :lecturer %>
<%= f.text_field :lecturer %>
</p>
<p>
<%= f.fields_for :students do |s| %>
<%= s.label :name %>
<%= s.text_field :name %>
<% end %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我的問題是如何計劃和學生(分配給它)用一種形式來保存
您正在嘗試執行嵌套窗體。看看這個[Ryan Bates關於嵌套表單的視頻](http://railscasts.com/episodes/196-nested-model-form-revised)。 –