【发布时间】:2015-10-04 04:26:30
【问题描述】:
注意:
我把这个应用的所有源代码都放在了here。
我的 schema.rb 文件中有这个:
create_table "courses", force: :cascade do |t|
t.integer "teacher_id"
t.integer "student_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quantity"
end
create_table "students", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "teachers", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的老师模型:
class Teacher < ActiveRecord::Base
has_many :courses
has_many :students, :through => :courses
end
我的学生模型:
class Student < ActiveRecord::Base
has_many :courses
has_many :teachers, :through => :courses
end
我的课程模型:
class Course < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
end
我的教师控制器中有空的add_student 方法,如下所示:
def add_student
end
views/teachers文件夹中的add_student.html.erb:
hello from add_student!
<%= form_for(@course) do |f| %>
<% if @course.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@course.errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% @course.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.select :student_id, Student.all.collect { |p| [ p.name, p.id ] } %>
</div>
<div class="field">
<%= f.number_field :quantity %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我想在这个add_student 中做的是相应地添加students 和quantity。
但我收到此错误:
First argument in form cannot contain nil or be empty
指的是这一行:
<%= form_for(@course) do |f| %>
我的问题:
如何在下拉列表中显示所有
students名称并在此add_student方法中给它们quantity?如何使用 POST 请求(如 API)来实现这一点,这样我就可以像这样简单地 POST 参数:
[{"student_id":1, "quantity":2},{"student_id":2, "quantity":3}]
- 如何使用表单和
PUT请求为此add_student制作update方法,以便我可以传递相同样式的参数?
[{"student_id":1, "quantity":2},{"student_id":2, "quantity":3}]
注意#1:
这是我所有的路线:
Prefix Verb URI Pattern Controller#Action
root GET / courses#index
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PATCH /courses/:id(.:format) courses#update
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PATCH /students/:id(.:format) students#update
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
teachers GET /teachers(.:format) teachers#index
POST /teachers(.:format) teachers#create
new_teacher GET /teachers/new(.:format) teachers#new
edit_teacher GET /teachers/:id/edit(.:format) teachers#edit
teacher GET /teachers/:id(.:format) teachers#show
PATCH /teachers/:id(.:format) teachers#update
PUT /teachers/:id(.:format) teachers#update
DELETE /teachers/:id(.:format) teachers#destroy
GET /teachers/:id/add_student(.:format) teachers#add_student
注意事项 #2
我把这个应用的所有源代码都放在了here。
【问题讨论】:
-
这个错误是由于你没有在
add_student方法中初始化@course -
@Pavan 我将
@course放在add_student方法中,但仍然出现错误。你能看看我的 github repo 并帮我初始化@course吗?
标签: ruby-on-rails ruby ruby-on-rails-4 associations has-many-through