[英]Nested forms in Devise (Rails 3.2) - Not saving
I have setup an app using the Devise gem to login (User model). 我已经使用Devise gem登录并设置了一个应用程序(用户模型)。 Within the sign up view, ive included a simple_fields_for form which includes the attributes from the Student model.
在注册视图中,IVE包含了一个simple_fields_for表单,其中包含Student模型的属性。
Everything displays correctly in the view, but when the submit button is clicked the nested record is not created. 一切都将在视图中正确显示,但是单击“提交”按钮时,不会创建嵌套记录。
User model 用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your 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) do |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)) do |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 do |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 much every post on here, and still cannot find a way to get the nested model to save when clicking the submit button. 我已经仔细阅读了这里的每篇文章,但仍然无法找到一种方法来保存嵌套模型,方法是单击“提交”按钮。 The user record is created without problem, but the student details do not save.
创建用户记录没有问题,但学生详细信息不会保存。 If anyone has any advice I would be hugely grateful.
如果有人有任何建议,我将不胜感激。 Im sure its something very simple that I'm missing but Ive been wrestling with this all day!
我肯定这很简单,我很想念,但是我整天都在努力!
Best Wishes, 最好的祝愿,
Mike 麦克风
Have you tried with an "f." 您是否尝试过“ f”。 on the front of your simple_fields_for?
在您的simple_fields_for的前面?
<%= f.simple_fields_for :student do |s| %>
See: https://github.com/plataformatec/simple_form#simple-fields-for 参见: https : //github.com/plataformatec/simple_form#simple-fields-for
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.