Ruby 1.9.3を使用してRails 3.2.0で設定しようとして
has_and_belongs_to_many
います.2つのテーブル間の関係を設定しようとしています.外部キーと私のモデルの両方で適切な結合テーブルの移行が設定されています.
学生.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 メソッドで保存できるようにしようとしていますが、ヘルプを探すたびにさまざまなエラーが発生し、解決策を見つけることができません
ご協力ありがとうございました