[英]How to perform create and update in has_many :through association?
Note : 注意事项 :
I put all of my source code for this app here . 我在这里放置了该应用程序的所有源代码。
I have this inside my schema.rb file: 我的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
My teacher model: 我的老师模型:
class Teacher < ActiveRecord::Base
has_many :courses
has_many :students, :through => :courses
end
My student model: 我的学生模型:
class Student < ActiveRecord::Base
has_many :courses
has_many :teachers, :through => :courses
end
My course model: 我的课程模型:
class Course < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
end
And I have empty add_student
method inside my teachers_controller like this: 而且我的教师add_student
有一个空的add_student
方法,如下所示:
def add_student
end
The add_student.html.erb inside views/teachers folder: 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 %>
What I'm trying to do inside this add_student
is adding students
and quantity
accordingly. 我想在这个add_student
内部add_student
是相应地增加students
和quantity
。
But I get this error: 但是我得到这个错误:
First argument in form cannot contain nil or be empty
Which refers to this line: 引用此行:
<%= form_for(@course) do |f| %>
My questions : 我的问题 :
How to display all students
name in a dropdown and give them quantity
inside this add_student
method? 如何在下拉菜单中显示所有students
姓名,并在此add_student
方法内给他们提供quantity
?
How to make this also works using POST request (like an API) so that I can simply POST params like this: 如何使它也可以使用POST请求(例如API)起作用,以便我可以像这样简单地进行POST参数:
[{"student_id":1, "quantity":2},{"student_id":2, "quantity":3}]
update
method for this add_student
using form and PUT
request so that I can pass the same style of params like such? 如何使用表单和PUT
请求为此add_student
设置update
方法,以便我可以传递类似样式的参数? [{"student_id":1, "quantity":2},{"student_id":2, "quantity":3}]
Note #1 : 注意#1 :
This is all my routes: 这是我所有的路线:
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
Note #2 笔记2
I put all of my source code for this app here . 我在这里放置了该应用程序的所有源代码。
pleas do this in your teachers_controller 请在您的teacher_controller中执行此操作
def add_student
@course = Course.new
end
currently your method is empty and @course is nil 当前您的方法为空,@ course为零
To make post or update request you need to pass url and method options to form_for tag. 要发出发布或更新请求,您需要将url和方法选项传递给form_for标记。 Follow followings step:- 遵循以下步骤:
overwrite the from tag with options value as:- 用以下选项值覆盖from标记:-
To create the record: <%= form_for(@course, url: courses_path, method: :post) do |f| 要创建记录:<%= form_for(@course,url:course_path,method::post)做| f | %> %>
To update the record: <%= form_for(@course, url: course_path(@course), method: :put) do |f| 更新记录:<%= form_for(@course,url:course_path(@course),方法::put)do | f | %> %>
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.