首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >rails 4设计出4种专用的消毒液

rails 4设计出4种专用的消毒液
EN

Stack Overflow用户
提问于 2016-10-11 19:06:17
回答 1查看 160关注 0票数 0

我在我的rails 4应用程序中使用了Devise,我有一些额外的字段,用户在设置密码时需要填写这些字段。

我创建了邀请控制器,并将额外的字段添加到控制器中的update_sanitized_params方法中。当我填写服务器输出给我的表单时:

代码语言:javascript
运行
复制
Started PUT "/users/invitation" for 127.0.0.1 at 2016-10-11 12:57:34 -0600
Processing by InvitationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"c0c75noldd9hB6pHjLh1A74KWsWGrIc/K4s7PUJkmtnCG9Uz3YMEJMiBKSpC8Fk+ObC67oBx6E5AxS0R/xZlUA==", "user"=>{"f_name"=>"Steve", "l_name"=>"Jenkinson", "date_of_birth"=>"1975-01-01", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Accept Invitation"}
  Account Load (1.0ms)  SELECT  "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain" = $1 LIMIT $2  [["subdomain", "10-106security"], ["LIMIT", 1]]
  Rendering devise/invitations/edit.html.erb within layouts/application
  Rendered devise/invitations/edit.html.erb within layouts/application (2.9ms)
  Rendered shared/_signed_out_nav.html.erb (1.4ms)
Completed 200 OK in 60ms (Views: 53.1ms | ActiveRecord: 2.0ms)

但是,没有保存用户属性。这在rails控制台中得到了验证。

控制台在提交邀请编辑表单后输出:

代码语言:javascript
运行
复制
  User Load (0.4ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => #<User id: 3, email: "test1@test.com", f_name: nil, l_name: nil, date_of_birth: nil, employee_number: nil, system_id: nil, created_at: "2016-10-11 19:33:33", updated_at: "2016-10-11 19:33:33", invitation_token: "d7024926f142aeeec1d23781f1832f74317b592f5bcdd5e6a3...", invitation_created_at: "2016-10-11 19:33:33", invitation_sent_at: "2016-10-11 19:33:33", invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: "User", invited_by_id: 1, invitations_count: 0>

我的应用程序控制器:

代码语言:javascript
运行
复制
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  # Before Actions
  before_filter :configure_permitted_parameters, if: :devise_controller?
  before_action :load_schema, :authenticate_user!, :set_mailer_host

  # Custom Methods

  private

  def load_schema
    Apartment::Tenant.switch!('public')
    return unless request.subdomain.present?

    if current_account
      Apartment::Tenant.switch!(current_account.subdomain)
    else
      redirect_to root_url(subdomain: false)
    end
  end

  def current_account
    @current_account ||= Account.find_by(subdomain: request.subdomain)
  end
  helper_method :current_account

  def after_sign_out_path_for(resource_or_scope)
    new_user_session_path
  end

  def set_mailer_host
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
  end

  def after_invite_path_for(resource)
    users_path
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:accept_invitation, keys: [:f_name, :l_name, :date_of_birth, :password, :password_confirmation, :invitation_token])
  end

end

以下是表格:

代码语言:javascript
运行
复制
<%= bootstrap_form_for resource, :as => resource_name, :url => invitation_path(resource_name), method: :put do |f| %>
  <%= f.text_field :f_name, label: 'First Name' %>
  <%= f.text_field :l_name, label: 'Last Name' %>
  <%= f.date_field :date_of_birth, label: 'Date Of Birth' %>
  <%= f.password_field :password, label: 'Password' %>
  <%= f.password_field :password_confirmation, label: 'Confirm Password' %>
  <%= f.submit "Accept Invitation", :class => 'btn btn-primary' %>
<% end %>

以下是用户模型:

代码语言:javascript
运行
复制
class User < ApplicationRecord
  # Before Actions

  # Devise Modules
  devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable, :timeoutable

  # Relationships

  # Validations
  validates :f_name, presence: true
  validates :l_name, presence: true
  validates :date_of_birth, presence: true

  # Custom Methods
  def full_name
    l_name.upcase + ", " + f_name
  end

end

这里的任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-12 03:50:38

这里的问题是,在编辑用户填写的devise_invitable表单时,我没有添加身份验证令牌。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39984852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档