如何获得设计工作与accept_nested_attributes_for有一个关系? [英] How to get devise to work with accepts_nested_attributes_for in a has one relationship?

查看:179
本文介绍了如何获得设计工作与accept_nested_attributes_for有一个关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试取得我的用户表单,也允许用户通过form_for同时填写公司简介。由于某种原因,它没有显示公司领域。这是我的控制器和布局的代码。

I am trying to get my user form to also allow the user to fill out their company profile at the same time via form_for. For some reason it is not showing the company fields. Here is my code for the controller and layouts.

class User < ActiveRecord::Base
  attr_accessible :company_attributes

  has_one :company
  accepts_nested_attributes_for :company
end

class Company < ActiveRecord::Base
  belongs_to :user

  # Validation
  validates :name, :presence => true
end

<%= f.fields_for :company do |company_form| %>
  <div class="field">
    <%= company_form.label :name, "Company Name" %><br />
    <%= company_form.text_field :name %>
  </div>
<% end %>


推荐答案

公司用户的c $ c>属性不应该是 - nil ,所以在控制器或窗体中,创建它:

The company attribute of the User should be not-nil, so either in the controller or in the form, create it:

<% user.build_company if user.company.nil? %>
<%= f.fields_for :company do |company_form| %>
...

这篇关于如何获得设计工作与accept_nested_attributes_for有一个关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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