使一个fields_for块有条件 [英] Make a fields_for block conditional

查看:201
本文介绍了使一个fields_for块有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型和一个教师模型。用户和教师之间存在一对一的关系。一些用户将是教练,有些用户不会。因此,我有一个使用fields_for方法写入两者的注册表单。



只有在他们说他们是教练的情况下,才能写入教师表,例如通过复选框。当他们写信时,我想保持我的表格的验证以及其余的表单



理想情况下,如果我能做到这一点通过模型,但我可以接受所有的建议。



讲师模型

  class Instructor< ActiveRecord :: Base 
belongs_to:user
validates_presence_of:school_url,...等...
attr_accessible:school_url,...等...
end

用户模型

  class User< ; ActiveRecord :: Base 
has_one:instructor,:dependent => :destroy

validates_uniqueness_of:email
validates:email,:confirmation => true

accepted_nested_attributes_for:Instructor


attr_accessible:email,:password,:instructor_attributes,,etc
end

HAML中的表单

   - 资源。 build_instructor 
- form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f |
= hidden_​​field_tag:destination,{:value =>目的地}
.field
= f.label:firstname,First Name
= f.text_field:firstname
.field
= f.label:lastname, 姓氏
= f.text_field:lastname
.field
= f.label:email,电子邮件
= f.email_field:电子邮件
.field
= f.label:email_confirmation,确认电子邮件
= f.email_field:email_confirmation
.field
= f.label:password
= f.password_field:password
.field
= f.label:password_confirmation,确认密码
= f.password_field:password_confirmation
#instructor-box
%p
%span.bold你是教练吗?
= check_box_tag:instructor_check
%span是的,我是教练
= f.fields_for:instructor do | i |
= render/ users / registrations / Instructor,:form => i


解决方案

我修复了问题。现在似乎太明显了为了使fields_for被取消,我只需删除由控制器中的表单创建的instructor_attributes。例如:



创建

 #params [:user] = > {:email => justin@example.edu,...,:instructor_attributes => {:school_url => www.example.edu,...} 
#params [:instructor_check] => 0

给定这些参数传递,我可以轻松删除要保存的属性,导轨不再尝试为与用户关联的教师创建新记录。这是字面上我使用的代码。不是最优雅,但它的作品。

  params [:user] .delete:instructor_attributes if params [:instructor_check] = 0

这意味着没有为用户创建教师资料,因此不会写入表。在发回空白属性并在验证失败之前。


I have a User Model and an Instructor Model. There is a one-to-one relationship between user and instructor. And some users will be instructors and some will not. As such I have a registration form that uses a fields_for method to write to both.

How can I write to the instructor table only on the condition that they say they are an instructor, such as through a checkbox. And when they do write I want to maintain my validations of the table along with the rest of the form

Ideally this would work best if I can do this through the model, but I'm open to all suggestions.

Instructor Model

class Instructor < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :school_url, :etc...
  attr_accessible :school_url, :etc...
end

User Model

class User < ActiveRecord::Base
  has_one :instructor, :dependent => :destroy

  validates_uniqueness_of :email
  validates :email, :confirmation => true

  accepts_nested_attributes_for :instructor


  attr_accessible :email, :password, :instructor_attributes, :etc
end

Form in HAML

- resource.build_instructor
- form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
  = hidden_field_tag :destination, { :value => destination}
    .field
      = f.label :firstname, "First Name"
      = f.text_field :firstname
    .field
      = f.label :lastname, "Last Name"
      = f.text_field :lastname
    .field
      = f.label :email, "E-Mail"
      = f.email_field :email
    .field
      = f.label :email_confirmation, "Confirm E-Mail"
      = f.email_field :email_confirmation
    .field
      = f.label :password
      = f.password_field :password
    .field 
      = f.label :password_confirmation, "Confirm Password"
      = f.password_field :password_confirmation
  #instructor-box
    %p
      %span.bold Are you an instructor?
      = check_box_tag :instructor_check
      %span Yes, I am an instructor
    = f.fields_for :instructor do |i|
      = render "/users/registrations/instructor", :form => i

解决方案

I fixed the problem. It seems all too obvious now. In order for the fields_for to be cancelled out all I have to do is delete the instructor_attributes that are being created by the form in the controller. For example:

Create

# params[:user] => {:email => "justin@example.edu", ..., :instructor_attributes => { :school_url => "www.example.edu", ...}
# params[:instructor_check] => "0"

Given these parameters being passed, I can easily delete the attributes that are to be saved and the rails no longer tries to create a new record for instructor that's to be associated with user. This is literally the code I used. Not the most elegant, but it works.

params[:user].delete :instructor_attributes if params[:instructor_check] = "0"

This recognizes that no instructor profile is being created for the user and thus does not write to the table. Before it was sending back blank attributes and failing on the validations.

这篇关于使一个fields_for块有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆