【问题标题】:Rails - nested model inside devise modelRails - 设计模型内的嵌套模型
【发布时间】:2016-04-12 14:18:43
【问题描述】:

我有两个模型,成员和公司... 成员是设计模型...当我注册时,我想在注册表单中包含以下字段。

  1. 姓名
  2. 电子邮件地址
  3. 密码
  4. 公司名称(来自公司模型)
  5. 公司类型(来自公司模型)

会员拥有一家公司

我正在尝试通过嵌套表单创建注册表单。但我不确定为公司构建表单以接收用户的输入...

这是我的控制器

class Brands::Members::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    @company = Company.new
    super
  end

  # POST /resource
  def create
    @company = Company.new(configure_sign_up_params)
    @company.valid?

    super
  end
end

这是我的观点

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <!--
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>
  -->
<%= @company.errors %>
  <%= fields_for @company do |fc| %>
    <div class="field">
      <%= fc.label :name %><br />
      <%= fc.text_field :name %>
    </div>

  <% end %>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "brands/members/shared/links" %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    希望这会有所帮助,

    将您的新操作更改为此

    @member = Member.new
    @member.build_company
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多