繁体   English   中英

在Rails 4上进行设计的用户模型的嵌套属性有错误吗?

[英]Error with nested attributes for a users model with devise on Rails 4?

我正在尝试在主模型的更新页面上为属于我的主模型的模型创建一条新记录,但是没有将其保存到数据库中。 基本上,公司模型充当主要用户模型,并且能够在devdesign生成的编辑注册页面上为其自己创建新的董事会成员。 这就是我所拥有的。

1)我的公司模式有很多董事会成员

class Company < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
    has_many :boardmembers
    accepts_nested_attributes_for :boardmembers
end

2)我的董事会成员模型

class Boardmember < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
    belongs_to :company
end

3)公司负责人

class Companies::RegistrationsController     < Devise::RegistrationsController
    prepend_before_filter :require_no_authentication, only: [ :new, :create, :cancel ]
    prepend_before_filter :authenticate_scope!, only: [:edit, :update, :destroy]


def create
    build_resource(sign_up_params)
    if resource.save
        redirect_to edit_company_registration_path
    else
        clean_up_passwords resource
        respond_with resource
    end
end


  def update
     # For Rails 4
     account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
      # For Rails 3
     # account_update_params = params[:user]

     # required for settings form to submit when password is left blank
     if account_update_params[:password].blank?
       account_update_params.delete("password")
       account_update_params.delete("password_confirmation")
     end

     @company = Company.find(current_company.id)

     # saves the companies boardmembers
     if @company.update_attributes(account_update_params)
       set_flash_message :notice, :updated
       # Sign in the user bypassing validation in case his password changed
       sign_in @company, :bypass => true
       redirect_to company_home_path
     else
       render 'edit'
     end
   end

end

3)和我在其中配置参数的应用程序控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:email, :name, :address, :jurisdiction_of_incorporation, :entity_type, :password,            :password_confirmation, boardmembers_attributes: [:company_id, :id, :email, :name, :address, :secondary_email,   :primary_phone, :password])
    end
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:email, :name, :password, :password_confirmation)
   end
end

结束

我的表单如下所示:edit.html.erb

 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
     <!-- here is where all the company update fields go -->
           ...
    <!-- here are the fields for creating boardmembers
    <%= f.fields_for :boardmembers, resource.boardmembers.build do |boardmember| %>

          <%= boardmember.text_field :name %>

            <%= boardmember.text_field :address %>


            <%= boardmember.text_field :primary_phone %>

            <%= boardmember.text_field :email %>

            <%= boardmember.text_field :secondary_email %>

            <%= boardmember.password_field :password %>
         <% end %>
   <%= f.submit "Update Your Account" %>

<% end %>

但是,公司会更新其记录,但根本不会创建新的董事会成员。 当我单击按钮时,我什至尝试渲染参数的json文件,结果看起来像

   {"utf8":"✓","_method":"put","authenticity_token":"mK4yd8t4m7N5rdfmHG8XKc/c+vNUdO8vryk5kYm7juw=","company":   {"email":"pizzahut@email.com","name":"Pizza Comp","password":"","password_confirmation":"","entity_type":"","jurisdiction_of_incorporation":"","address":"","boardmembers_attributes":{"0":{"name":"","address":"","primary_phone":"","email":"","secondary_email":"","password":""}}},"commit":"Update Your Account","action":"update","controller":"companies/registrations"}

董事会成员的所有参数都是空的,即使我填写了这些参数也是如此。我已经尝试了所有教程并在线回答,但在这种情况下它们似乎都不起作用。 还有什么呢? 有任何想法吗? 永远不会创建新记录。 请帮忙。

尝试这个

<%= form_for([resource,resource.with_boardmembers], as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

        <!-- here is where all the company update fields go -->

       ...
       <!-- here are the fields for creating boardmembers

      <%= f.fields_for :boardmembers do |boardmember| %>

      <%= boardmember.text_field :name %>

        <%= boardmember.text_field :address %>


        <%= boardmember.text_field :primary_phone %>

        <%= boardmember.text_field :email %>

        <%= boardmember.text_field :secondary_email %>

        <%= boardmember.password_field :password %>
     <% end %>

在您的用户模型中添加

def with_boardmembers
   self.boardmembers.build
   self
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM