1

我正在尝试使用 ruby​​ 1.9.3 在 rails 3.2.0 中设置 has_and_belongs_to_many两个表之间的关系我使用两个外键设置了正确的连接表迁移,我的模型就是这样

学生.rb

class Student < ActiveRecord::Base
  attr_accessible :birthday, :id, :name, :year, :course_ids

  has_and_belongs_to_many :courses

  accepts_nested_attributes_for :courses
end

课程.rb

class Course < ActiveRecord::Base
  attr_accessible :id, :coursePrefix, :roomNumber, :name, :student_ids
  has_and_belongs_to_many :students
end

_form.html.erb

<%= form_for(@student) do |f| %>
  <% if @student.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@student.errors.count, "error") %> prohibited this student from being saved:</h2>

      <ul>
      <% @student.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :id %><br />
    <%= f.text_field :id %>
  </div>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :birthday %><br />
    <%= f.date_select :birthday, :start_year => 1930 %>
  </div>
  <div class="field">
    <%= f.label :year %><br />
    <%= f.text_field :year %>
  </div>
  <div>
    <%= f.collection_select(:courses, @courses, :id, :name)%>
  </div>
  <br />
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

和 student_controller.rb

# GET /students/new
  # GET /students/new.json
  def new
    @student = Student.new
    @courses = Course.all
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @student }
    end
  end

  # GET /students/1/edit
  def edit
    @student = Student.find(params[:id])
    @courses = Course.all
  end

  # POST /students
  # POST /students.json
  def create
    @student = Student.new(params[:student])

    respond_to do |format|
      if @student.save
        format.html { redirect_to @student, notice: 'Student was successfully created.' }
        format.json { render json: @student, status: :created, location: @student }
      else
        format.html { render action: "new" }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /students/1
  # PUT /students/1.json
  def update
    @student = Student.find(params[:id])
    @courses = @student.courses.find(:all)

    respond_to do |format|
      if @student.update_attributes(params[:student])
        format.html { redirect_to @student, notice: 'Student was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

我再次尝试获取表单元素以允许学生选择课程并在 JOIN 方法下保存在该表中,但是每次我尝试寻求帮助时都会遇到不同的错误并且找不到解决方案

感谢您的帮助

4

1 回答 1

0

您只需授予对 Student 模型的 :courses 属性的访问权限:

class Student < ActiveRecord::Base
  attr_accessible :birthday, :id, :name, :year, :course_ids, :courses
  #                                                           ^ ^ ^ ^

为什么?因为您的控制器接收格式如下的参数:

params = {
  student: {
    id: 12,
    name: "Bob the Sponge",
    courses: [5, 7], # Course ids
    etc: ...
  }
}

并且控制器试图直接使用参数创建一个学生对象,所以当他试图更新courses学生的属性时,它不能,因为你没有将它定义为可访问的。

于 2013-10-01T14:30:14.960 回答