Rails HABTM显示和选择

时间:2013-10-01 14:08:13

标签: sql ruby-on-rails ruby-on-rails-3 has-and-belongs-to-many

我正在尝试使用ruby 1.9.3 a在rails 3.2.0中进行设置 has_and_belongs_to_many两个表之间的关系我有正确的连接表迁移设置与外键和我的模型是这样的

student.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

course.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方法下保存在该表中,但是每当我尝试寻求帮助时,我会得到不同的错误而无法找到解决方案

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您只需要访问学生模型的: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: ...
  }
}

并且控制器正在尝试使用params直接创建Student对象,因此当他尝试更新Student的courses属性时,它不能,因为您没有将其定义为可访问。