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

时间:2015-07-15 08:24:37

标签: ruby-on-rails ruby ruby-on-rails-4 associations has-many-through

注意

我将此应用的所有源代码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内尝试做的是相应地添加studentsquantity

但是我收到了这个错误:

First argument in form cannot contain nil or be empty

这是指这一行:

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

我的问题

  1. 如何在下拉列表中显示所有students名称并在quantity方法中为其add_student提供?

  2. 如何使用POST请求(如API)也可以使用,这样我就可以像这样简单地发送params:

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

    1. 如何使用表单和update请求为此add_student创建PUT方法,以便我可以传递相同类型的params?
    2. [{"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

2 个答案:

答案 0 :(得分:0)

请在您的teachers_controller中执行此操作

 def add_student
    @course = Course.new
 end

目前你的方法是空的,而@course是零

答案 1 :(得分:0)

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

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

    创建记录:&lt;%= form_for(@course,url:courses_path,method :: post)do | f | %GT;

    要更新记录:&lt;%= form_for(@course,url:course_path(@course),方法:: put)do | f | %GT;

  3. 由于