繁体   English   中英

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

[英]How to perform create and update in has_many :through association?

注意事项

在这里放置了该应用程序的所有源代码。


我的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有一个空的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内部add_student是相应地增加studentsquantity

但是我得到这个错误:

First argument in form cannot contain nil or be empty

引用此行:

<%= form_for(@course) do |f| %>

我的问题

  1. 如何在下拉菜单中显示所有students姓名,并在此add_student方法内给他们提供quantity

  2. 如何使它也可以使用POST请求(例如API)起作用,以便我可以像这样简单地进行POST参数:

[{"student_id":1, "quantity":2},{"student_id":2, "quantity":3}]

  1. 如何使用表单和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

在这里放置了该应用程序的所有源代码。

请在您的teacher_controller中执行此操作

 def add_student
    @course = Course.new
 end

当前您的方法为空,@ course为零

要发出发布或更新请求,您需要将url和方法选项传递给form_for标记。 遵循以下步骤:

  1. 在add_student方法内,初始化@course变量。
  2. 用以下选项值覆盖from标记:-

    要创建记录:<%= form_for(@course,url:course_path,method::post)做| f | %>

    更新记录:<%= form_for(@course,url:course_path(@course),方法::put)do | f | %>

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM