简体   繁体   English

如何在has_many中通过关联执行创建和更新?

[英]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是相应地增加studentsquantity

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 : 我的问题

  1. How to display all students name in a dropdown and give them quantity inside this add_student method? 如何在下拉菜单中显示所有students姓名,并在此add_student方法内给他们提供quantity

  2. 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}]

  1. How to make the 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:- 遵循以下步骤:

  1. Inside add_student method, initialize @course variable. 在add_student方法内,初始化@course变量。
  2. 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.

相关问题 如果涉及多态关联,如何创建“ has_many through”关联? - How to create a 'has_many through' association if a polymorphic association is involved? 如何通过关联更新Has_Many的记录? - How do I update the record of a Has_Many through association? 如何通过关联使用 has_many 自动更新模型? - How to automatically update model with has_many through association? 创建并删除has_many:through关联 - CREATE AND DELETE The has_many :through Association 如何通过关联为has_many创建嵌套资源的表单? - How to create form for nested resource with has_many through association? 如何通过Rails 3.2中的关联在has_many中创建记录 - How to create records in has_many through association in rails 3.2 has_many:through association 的验证检查有问题; 如何创造? - Trouble with validation check for has_many :through association; how to create? 如何在has_many的中间模型中创建has_many关联:以最佳方式通过关联Rails - How to create has_many associations in the intermediate model of has_many :through association in an optimal way Rails has_many:通过关联 - The has_many :through Association 我如何使用form_for更新关联的has_many:through关联 - How can I use form_for to update an association's has_many :through association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM