【问题标题】:How to perform create and update in has_many :through association?如何在 has_many 中执行创建和更新:通过关联?
【发布时间】: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 中做的是相应地添加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

我把这个应用的所有源代码都放在了here

【问题讨论】:

  • 这个错误是由于你没有在add_student方法中初始化@course
  • @Pavan 我将@course 放在add_student 方法中,但仍然出现错误。你能看看我的 github repo 并帮我初始化@course吗?

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


【解决方案1】:

请在您的教师控制器中执行此操作

 def add_student
    @course = Course.new
 end

目前你的方法是空的,@course 是 nil

【讨论】:

  • 在你的表单中你还需要隐藏字段来设置teacher_id .
  • 好的。但是当我尝试将新学生分配给老师时,我得到了这个:i.imgur.com/N5KTYXa.png。当我检查我的数据时,teacher_id 的值是null。有什么想法吗?
  • 您如何将teacher_id 分配给学生。使用隐藏字段或其他方式。可以分享一下吗?
  • 我把这个 &lt;%= f.hidden_field :teacher_id %&gt; 放在我的 add_student.html.erb 文件中,就像你说的那样。那是对的吗?顺便说一句,我把我所有的源代码都放在了我的 Github 仓库github.com/zulhfreelancer/school
  • #请将teacher.id放在:teacher_id之后。
【解决方案2】:

要发布或更新请求,您需要将 url 和方法选项传递给 form_for 标签。请按照以下步骤操作:-

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

    创建记录:

    更新记录:

谢谢

【讨论】:

  • 我明白了 - i.imgur.com/54AqX2e.png。好像不能捕获教师ID吧?
  • 您需要将隐藏字段teacher_id包含为
猜你喜欢
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
相关资源
最近更新 更多