【问题标题】:Validating then using virtual attributes to create a devise user验证然后使用虚拟属性创建设计用户
【发布时间】:2017-06-16 13:19:35
【问题描述】:

所以我有两个模型,用户和员工。用户有一名员工,员工属于用户。我想创建一个员工,但首先我必须创建一个新用户。我的 Employee 模型没有属性 :email, :password, :password_confirmation 所以我创建了虚拟属性。这是弹出的错误Validation failed: Email is invalid, Password confirmation doesn't match Password

这是我的员工模型

class Employee < ApplicationRecord
belongs_to :user

attr_accessor :email, :password, :password_confirmation
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
validates :password, confirmation: true
end

我的员工控制器

class EmployeesController < ApplicationController
  def create
    @newuser=User.create!(
      email: :email,
      password: :password,
      password_confirmation: :password_confirmation
    )

    @employee = Employee.new(employee_params)
    respond_to do |format|
      if @employee.save
        format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
        format.json { render :show, status: :created, location: @employee }
      else
        format.html { render :new }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

private
  def employee_params
    params.require(:employee).permit(:name, :contact_no, :role_id, @newuser.id)
  end
end

还有我的表格

<%= form_for(employee) do |f| %>
  <% if employee.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(employee.errors.count, "error") %> prohibited this employee from being saved:</h2>

      <ul>
      <% employee.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  
  <div class="field">
    <%= f.label :email %>
    <%= f.email_field :email %>
  </div>
  
  <div class="field">
    <%= f.password_field :password %>
    <%= f.password_field :password_confirmation %>
  </div>
  
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= f.label :contact_no %>
    <%= f.text_field :contact_no %>
  </div>

  <div class="field">
    <%= f.label :role_id %>
    <%= f.number_field :role_id %>
  </div>

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

我仍在学习 Rails,非常感谢您的帮助

【问题讨论】:

    标签: html ruby-on-rails devise


    【解决方案1】:

    如果您没有属性 :email, :password, :password_confirmation 则删除以下验证:

    validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
    validates :password, confirmation: true
    

    来自员工模型。

    【讨论】:

    • 我这样做了,但随后抛出此错误Validation failed: Email is invalid, Password confirmation doesn't match Password
    • 您是否从员工模型中删除了验证并重新启动服务器
    • 是的,删除验证并重新启动服务器。但它说验证失败。我不知道在员工模型中没有进行验证时它是如何失败的
    【解决方案2】:

    我找到了解决问题的方法,似乎我的用户参数没有遵循 Rails 的强参数规则。所以我的控制器现在有了这个

    def employee_params
      params.require(:employee).permit(:name, :contact_no, :role_id)
    end
    
    def user_params
      params.require(:employee).permit(:email, :password, :password_confirmation)      
    end
    

    然后我可以让用户毫无问题地使用这些参数。

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多