【问题标题】:Error with nested attributes for a users model with devise on Rails 4?在 Rails 4 上设计的用户模型的嵌套属性错误?
【发布时间】:2014-06-06 23:25:26
【问题描述】:

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

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"}

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

【问题讨论】:

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


    【解决方案1】:

    试试这个

    <%= 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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多