2

私はレールが初めてです。なので、あまり理解できていません。3 つのテーブルを持つデータベースがあります。学生テーブル、コーステーブル、および登録テーブル。私の Railde モデルは次のとおりです。

class Student < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations
  has_many :courses, :through => :registrations

  validates :name, :presence => true

end

class Course < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations, :dependent => :destroy
  has_many :students, :through => :registrations

  validates :name , :presence => true
  validates :name, :uniqueness => true
end

class Registration < ActiveRecord::Base
  attr_accessible :course_id, :student_id


  belongs_to :course
  belongs_to :student

  validates :course_id, :student_id, :presence => true
  validates :course_id, :uniqueness => {:scope => :student_id}
  validates :student_id, :uniqueness => {:scope => :course_id}
end

...................................

Controller action for updating student :

  def update
    @student = Student.find(params[:id])
    if @student.update_attributes(params[:student])
      redirect_to students_path, :notice => "Registration completed"
    else
      render 'edit'
    end
  end

................ 意見 :

<%=form_for @student do |f| %>
    <p>
      <%= f.label :name, "Name" %>
      <%= f.text_field :name %>
    </p>
    <p>
      <%= render('course_list') %>
    </p>
    <p>
      <%= f.submit %>
    </p>
<% end %>

............... _course_list パーシャル :

Select Courses :<br/>

        <p>

          <% Course.all.each do |course| %>

              <%=check_box_tag "student[course_ids][]", course.id, `enter code here`@student.course_ids.include?(course.id) %>
              <%= course.name %>  <br/>

          <% end %>
    </p>

................................... 更新ボタンを送信すると、エラーが発生しました

保護された属性を一括割り当てできません: course_ids

.......

パラメーター :

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"j/lDE5bv1gWkfadQ6Cag6hGjg5nD2Ikad9vHOJTE7Pc=",
 "student"=>{"name"=>"Galib",
 "course_ids"=>["2",
 "3"]},
 "commit"=>"Update Student",
 "id"=>"6"} 

...................................

私がしたいのは、更新ボタンがクリックされた場合、学生テーブルと登録テーブルの両方を更新する必要があるということです。助けてください。

4

3 に答える 3

0

MongoDB の場合、Rails モデルで多対多has_and_belongs_to_manyを実装するには、単純なモデルの代わりに使用する必要がありますhas_many

于 2013-07-11T05:12:02.483 に答える