Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Desive user创建员工_Ruby On Rails_Ruby On Rails 4_Devise - Fatal编程技术网

Ruby on rails 使用Desive user创建员工

Ruby on rails 使用Desive user创建员工,ruby-on-rails,ruby-on-rails-4,devise,Ruby On Rails,Ruby On Rails 4,Devise,看起来像这样 但我的不同 我想创建员工记录,该员工可以选择是否拥有用户帐户。我正在为我的用户使用Desive gem 所以我有一个员工模型 has_one :user accepts_nested_attributes_for :user 和用户模型 belongs_to :employee 我知道我需要在视图中放置嵌套用户,但如何将其保存在控制器中 不是这样的,对吧 params.require(:employee).permit(:name, user_attributes: [:ema

看起来像这样 但我的不同

我想创建员工记录,该员工可以选择是否拥有用户帐户。我正在为我的用户使用Desive gem

所以我有一个员工模型

has_one :user
accepts_nested_attributes_for :user
和用户模型

belongs_to :employee
我知道我需要在视图中放置嵌套用户,但如何将其保存在控制器中

不是这样的,对吧

params.require(:employee).permit(:name, user_attributes: [:email, :password])

你能给我一些提示或一些有用的链接吗?这项设计很难修改。谢谢^ ^

希望我没有误解,但不需要修改设计。 只需传递所需的所有参数,如果有效,则直接创建用户:

params.require(:employee).permit(:name, 
                                 :user_wants_account, 
                                 :whatever, 
                                 :user => [:email, :password, :password_confirmation])

# manually
User.create(:email => params[:employee][:user][:email], 
            :password => params[:employee][:user][:password], 
            :password_confirmation => params[:employee][:user][:password_confirmation],
            :employee_id => self)
如果您想使用“默认”登录表单-没问题-因为每个用户都有员工关系…

不应修改;它只是一系列控制器和一个模型

如果您想创建一个
员工
,然后将数据传递给
用户
模型,那么您采取了正确的方法

您需要做的是保持惯例(即构建
用户
模型等),以确保设备的功能得到维护:



我没有任何理由或测试来反驳这一点。您可以使用
User.new
调用加载表单。因此,我猜测,你将面临的主要障碍是强大参数的通过。

那么,这为什么不起作用呢?params散列看起来怎么样?您的表单如何?它正在工作,但密码未加密。我认为这样做的代码不好。使用
构建资源(params[employee][:user][:email],params[employee][:user][:password])
怎么样?但是如果我的控制器是这样的
类employeescoontroller
谢谢。编辑怎么样?我还可以使用
员工参数吗?我将在哪里声明
当前\u密码
字段?因为我试图保存它,但它不起作用。
#app/controllers/employees_controller.rb
class EmployeesController < ApplicationController
   def new
      @employee = Employee.new
      @employee.build_user
   end

   def create
      @employee = Employee.new employee_params
      @employee.save
   end

   private

   def employee_params
      params.require(:employee).permit(:name, user_attributes: [:email, :password, :password_confirmation])
   end
end
#app/views/employees/new.html.erb
<%= form_for @employee do |f| %>
   <%= f.text_field :name %>
   <%= f.fields_for :user do |user| %>
      <%= user.email_field :email %>
      <%= user.password_field :password %>
      <%= user.password_field :password_confirmation %>
   <% end %>
   <%= f.submit %>
<% end %>