Ruby on rails Rails 4上带有Desive的用户模型的嵌套属性出错?
我试图在主模型的更新页面上为属于我的主模型的模型创建一个新记录,但该记录没有保存到数据库中。基本上,公司模型充当主要用户模型,它能够在Desive生成的编辑注册页面上为自己创建新的董事会成员。这是我的 我的公司模式有很多董事会成员Ruby on rails Rails 4上带有Desive的用户模型的嵌套属性出错?,ruby-on-rails,ruby,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,我试图在主模型的更新页面上为属于我的主模型的模型创建一个新记录,但该记录没有保存到数据库中。基本上,公司模型充当主要用户模型,它能够在Desive生成的编辑注册页面上为自己创建新的董事会成员。这是我的 我的公司模式有很多董事会成员 class Company < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :time
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
董事会成员的所有参数都是空的,即使我填写了它们。我在网上尝试了每一个教程和答案,但没有一个在这种情况下有效。还可能是什么?有什么想法吗?新记录永远不会被创建。请帮忙。试试这个
<%= 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 %>
我引用了这个-更新:工作。只是一个语法错误,我把它搞砸了。你的解决方案对一个模型有效,但当我尝试对另一个模型有效时,我明白了,我能做什么?
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
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
<%= 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 %>
{"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