ruby - Nested forms in Devise (Rails 3.2) - Not saving -
i have setup app using devise gem login (user model). within sign view, ive included simple_fields_for form includes attributes student model.
everything displays correctly in view, when submit button clicked nested record not created.
user model
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # setup accessible (or protected) attributes model attr_accessible :email, :password, :password_confirmation, :remember_me, :role, :student_attributes # attr_accessible :title, :body has_one :student, :inverse_of => :user, :autosave => true accepts_nested_attributes_for :student end
student model
class student < activerecord::base attr_accessible :course, :email, :fname, :lname, :student_num, :university, :year set_primary_key :student_num belongs_to :user, :inverse_of => :student, :autosave => true has_many :attendances has_many :teaching_sessions, through: :attendances has_many :completed_outcomes validates :student_num, presence: true validates :fname, presence: true validates :lname, presence: true validates :university, presence: true validates :course, presence: true validates :year, presence: true validates :email, presence: true def self.import(file) csv.foreach(file.path, headers: true) |row| student.create! row.to_hash end end def full_name "#{fname} #{lname}" end end
devise registration form
<h2>sign up</h2> <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) |f| %> <%= f.error_notification %> <h3>login details</h3> <div class="form-inputs"> <%= f.input :email, :required => true, :autofocus => true %> <%= f.input :password, :required => true %> <%= f.input :password_confirmation, :required => true %> <%= f.select :role, ["student","teacher","admin"], :required => true %> </div> <h3>profile</h3> <div id = "student_fields"> <%= simple_fields_for :student |s| %> <%= s.input :student_num %> <%= s.input :fname %> <%= s.input :lname %> <%= s.input :university %> <%= s.input :course %> <%= s.input :year %> <% end %> </div> <br /> <div class="form-actions"> <%= f.button :submit, "sign up" %> </div> <% end %> <br /> <%= render "devise/shared/links" %>
custom devise controller
class users::registrationscontroller < devise::registrationscontroller def new resource = build_resource({}) resource.build_student respond_with resource end def create super end def update super end end
ive read through pretty every post on here, , still cannot find way nested model save when clicking submit button. user record created without problem, student details not save. if has advice hugely grateful. im sure simple i'm missing ive been wrestling day!
best wishes,
mike
have tried "f." on front of simple_fields_for?
<%= f.simple_fields_for :student |s| %>
see: https://github.com/plataformatec/simple_form#simple-fields-for
Comments
Post a Comment