ruby on rails 4 mass assignment -
i have:
- a model user common data such email, password, first_name, last_name
- a model student specific data such faculty_id, etc..
what want have possibility modify student's profile, e.g change faculty , credentials. found out accepts_nested_attributes_for should me that. have:
class user < activerecord::base has_one :student has_one :header has_one :admin has_many :progresses, :through => :student accepts_nested_attributes_for :student end class student < activerecord::base belongs_to :user belongs_to :group has_one :specialization, through: :group has_many :progresses scope :activated, lambda {(where("users.activated = 'off'"))} self.per_page = 1 end class admin::studentscontroller < admincontroller before_action :set_user, only: [:show, :edit, :update, :destroy] def index @students = student.joins(:user).joins(:group).paginate(:page => params[:page]).all end def show end def edit end def update end private def set_user @student = student.find(params[:id]) @student.build_user end end
form partial
= form_for([:admin,@student]) |f| - if @student.errors.any? #error_explanation h2 = pluralize(@student.errors.count, "error") | prohibited group being saved: ul - @student.errors.full_messages.each |msg| li= msg .field = f.fields_for :user |builder| = builder.label :first_name br/ = builder.text_field :first_name .field = f.label :faculty_id br/ = f.text_field :faculty_id .actions = f.submit
i looked inspect element in browser , input user object
<input id="student_user_first_name" name="student[user][first_name]" type="text">
which seems correct. field empty. please tell me went wrong? thank you.
rails 4 uses strong parameters, need whitelist params in controller. here explanation considers use of accepts_nested_attribute_for. http://edgeapi.rubyonrails.org/classes/actioncontroller/strongparameters.html
Comments
Post a Comment