设计4将自定义添加的字段更新为用户模型

时间:2016-12-29 10:26:03

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

我使用Devise 4,Rails 5并拥有一个用户模型,并将以下字段添加到设计提供的标准用户模型中:

class AddFieldsToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :name, :string
    add_column :users, :username, :string
    add_column :users, :headline, :string
    add_column :users, :location, :string
    add_column :users, :description, :text
  end
end

在account_update页面中,我想让用户能够编辑这些字段以及密码,因此我在views/devise/registration/edit中设置了以下内容:

      <div class="row">
        <div class="col-xs-12 col-sm-8">
          <div class="card">
            <div class="card-header">
              <h6>Profile</h6>
            </div>

            <div class="card-block">
              <div class="form-horizontal">
                <div class="form-group">
                  <label for="input1" class="col-sm-2 control-label">Name</label>
                  <div class="col-sm-10">
                    <%= f.text_field :name, class:"form-control", id:"input1" %>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input2" class="col-sm-2 control-label">Username</label>
                  <div class="col-sm-10">
                    <%= f.text_field :username, class:"form-control", id:"input2" %>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input3" class="col-sm-2 control-label">Email</label>
                  <div class="col-sm-10">
                    <%= f.email_field :email, class:"form-control", id:"input3" %>
                  </div>
                </div>

                <hr>

                <div class="form-group">
                  <label for="input4" class="col-sm-2 control-label">Headline</label>
                  <div class="col-sm-10">
                    <%= f.text_field :headline, class:"form-control", id:"input4" %>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input5" class="col-sm-2 control-label">Location</label>
                  <div class="col-sm-10">
                    <%= f.text_field :location, class:"form-control", id:"input5" %>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input6" class="col-sm-2 control-label">Description</label>
                  <div class="col-sm-10">
                    <%= f.text_area :description, class:"form-control", id:"input6", rows:4 %>
                  </div>
                </div>


              </div>
            </div>
          </div>

          <div class="card">
            <div class="card-header">
              <h6>Passwords</h6>
            </div>

            <div class="card-block">
              <div class="form-horizontal">
                <div class="form-group">
                  <label for="input1" class="col-sm-2 control-label">New password</label>
                  <div class="col-sm-10">
                    <%= f.password_field :password, class:"form-control"%>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input2" class="col-sm-2 control-label">Confirm password</label>
                  <div class="col-sm-10">
                    <%= f.password_field :password_confirmation, class:"form-control"%>
                  </div>
                </div>

                <div class="form-group">
                  <label for="input3" class="col-sm-2 control-label">Current password</label>
                  <div class="col-sm-10">
                    <%= f.password_field :current_password, class:"form-control"%>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <%= f.submit "Update", class:"btn btn-primary btn-sm" %>
                  </div>
                </div>

              </div>
            </div>
          </div>

        </div>

        <div class="col-xs-12 col-sm-4">
          <div class="card">
            <div class="card-header">
              <h6>Avatar</h6>
            </div>

            <div class="card-block">

                <br>
                <button class="btn btn-primary btn-sm">Update</button>
            </div>
          </div>
        </div>
      </div>

      <% end %>

我已经遵循了4个强大的param更改设计,并且在application_controller.rb iv中得到了以下内容:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    added_attrs = [:name, :username, :location, :headline, :description, :email, :password, :password_confirmation]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs
  end

end

即使有以下内容,这似乎也没有更新字段?

在我的application_controller.rb我也试过了:

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :username, :location, :headline, :description])
  end

0 个答案:

没有答案