-4

新しいユーザーを作成するためのパラメーター値にアクセスするタスクを取得しました。作成用のコントローラーコードは

def newstudent
    @student = Student.new(params)
   puts "+++++++++++"
   puts params.inspect

    if @student.save 
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @jtable }
    end
    end
  end

しかし、これを行うことで、ターミナルでエラーが発生しました。次のように表示されます

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: action, controller):
  app/controllers/students_controller.rb:42:in `new'
  app/controllers/students_controller.rb:42:in `newstudent'

問題を解決するのを手伝ってください。

4

3 に答える 3

1

Rails 3.2.3 では、デフォルトで大量割り当てはありません。モデルに移動して、attr_accessible :name、:position、:visible を追加する必要があります。基本的に、一括割り当てするすべての属性を追加する必要があります。

 class Student < ActiveRecord::Base
    attr_accessible :name, :position, :visible
 end
于 2012-11-28T11:39:45.787 に答える
0

Student モデルは、次のように使用attr_accessibleして一括割り当てできる属性をホワイトリストに登録する必要があります。

class Student < ActiveRecord::Base
  attr_accessible :name
end
于 2012-11-28T11:11:18.733 に答える