【问题标题】:Custom field for devise on Rails 5在 Rails 5 上设计的自定义字段
【发布时间】:2017-09-12 10:57:49
【问题描述】:

我已经安装了 devise gem 并将 fullnamelocation 的自定义字段作为字符串添加到数据库中。

我将editnew 表单页面更新为:

<%= f.input :fullname, required: true %>
<%= f.input :location %>

但它不会保存或更新此字段。

我看不到任何控制器

我错过了什么?看了几十个教程,还是搞不懂。

我正在使用 Rails 5.1.3 和 Ruby 2.4.0p0。

【问题讨论】:

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


    【解决方案1】:

    您可以通过在过滤器之前使用configure_permitted_parameters 以“懒惰的方式”进行操作。

    在您的 ApplicationController 中添加受保护的方法,指定在 devise_parameter_sanitizer 中允许的密钥。如果正在使用的控制器是设计注册的控制器,则添加指向此方法的 before_action 回调。

    在你的情况下可能是这样的:

    class ApplicationController < ActionController::Base
      before_action :configure_permitted_parameters, if: :devise_controller?
    
      protected
    
      def configure_permitted_parameters
        permit_attrs(%i[fullname location])
      end
    
      def permit_attrs(attrs)
        %i[sign_up account_update].each do |action|
          devise_parameter_sanitizer.permit(action, keys: attrs)
        end
      end
    end
    

    【讨论】:

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