首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4质量分配

rails 4质量分配
EN

Stack Overflow用户
提问于 2014-03-06 09:01:04
回答 1查看 60关注 0票数 0

我有:

  • 通用数据(如电子邮件、密码、first_name、last_name )的模型用户
  • 特定数据(如faculty_id等)的模型学生。

我想要的是有可能修改学生的档案,例如改变它的教员和证书。我发现accepts_nested_attributes_for应该在这方面帮我。这就是我所拥有的:

代码语言:javascript
复制
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

形式部分

代码语言:javascript
复制
= form_for([:admin,@student]) do |f|
 - if @student.errors.any?
  #error_explanation
   h2
    = pluralize(@student.errors.count, "error")
    | prohibited this group from being saved:
   ul
    - @student.errors.full_messages.each do |msg|
      li= msg
  .field
   = f.fields_for :user do |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

我在浏览器中查看了检查元素,用户对象的输入是

代码语言:javascript
复制
<input id="student_user_first_name" name="student[user][first_name]" type="text">

这似乎是对的。但是田野是空的。请告诉我哪里出了问题?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-03-06 10:04:39

Rails 4使用强参数,因此需要在控制器中白化您的params。这里有一个很好的解释,它考虑了accepts_nested_attribute_for的使用。http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22219568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档