设计用户模型不更新

时间:2014-11-27 18:35:23

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

当我提交用户更新表单时,我没有收到任何错误,但信息不会更新。
这是我的RegistrationsController

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:username, :email, :password, :password_confirmation, :firstname, :lastname, :displayname)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  end
end

以下是我routes.rb

的一部分
devise_for :users, :controllers => { registrations: 'registrations' }

最后这里是devise/registrations/edit.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render 'shared/error_messages' %>
<h1>Edit Your Profile</h1>
<div class="row">
    <div class="col-md-4">
    <h3>Social</h3>
        <hr>
        <div class="form-group">
            <%= f.label :displayname, "Display Name" %><br>
            <%= f.text_field :displayname, autocomplete: "off", class: "form-control" %>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-6">
                    <%= f.label :firstname, "First Name" %><br>
                    <%= f.text_field :firstname, class: "form-control" %>
                </div>
                <div class="col-md-6">
                    <%= f.label :lastname, "Last Name" %><br>
                    <%= f.text_field :lastname, class: "form-control" %>
                </div>
            </div>
        </div>

        <%= f.label :twitter, "Twitter" %><br>
        <div class="input-group">
            <div class="input-group-addon">twitter.com/</div>
            <%= f.text_field :twitter, class: "form-control" %>
        </div>
        <br>

        <%= f.label :facebook, "Facebook" %><br>
        <div class="input-group">
            <div class="input-group-addon">facebook.com/</div>
            <%= f.text_field :facebook, class: "form-control" %>
        </div>
        <br>

        <%= f.label :reddit, "Reddit" %><br>
        <div class="input-group">
            <div class="input-group-addon">reddit.com/u/</div>
            <%= f.text_field :reddit, class: "form-control" %>
        </div>
        <br>

        <%= f.label :github, "Github" %><br>
        <div class="input-group">
            <div class="input-group-addon">github.com/</div>
            <%= f.text_field :github, class: "form-control" %>
        </div>
        <br>

        <%= f.label :youtube, "Youtube" %><br>
        <div class="input-group">
            <div class="input-group-addon">youtube.com/user/</div>
            <%= f.text_field :youtube, class: "form-control" %>
        </div>
        <br>

        <%= f.label :twitch, "Twitch" %><br>
        <div class="input-group">
            <div class="input-group-addon">twitch.tv/</div>
            <%= f.text_field :twitch, class: "form-control" %>
        </div>

    </div>

    <div class="col-md-4">
    <h3>Login Info</h3>
        <hr>
            <div class="form-group">
                <%= f.label :email %><br>
                <%= f.email_field :email, autofocus: true, class: "form-control" %>
            </div>

            <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
            <div class="alert alert-warning">Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
            <% end %>

            <div class="form-group">
                <%= f.label :password %><br>
                <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
                <span class="help-block">Leave blank if you don't want to change it</span>
            </div>

            <div class="form-group"><%= f.label :password_confirmation %>
                <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
            </div>

            <div class="form-group"><%= f.label :current_password %>
                <%= f.password_field :current_password, autocomplete: "off", class: "form-control" %></div>
                <span class="help-block">Please enter your current password to confirm your changes</span>
            </div>

    <div class="col-md-4">
    <h3>Your Stats</h3>
        <hr>

    </div>
</div>
<br>
  <div class="form-group"><%= f.submit "Update", class: "btn btn-success" %></div>
<% end %>

<%= link_to "Back", :back %>

用户模型:

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable, :confirmable, :timeoutable, :lockable
  #ROLES = "%w[default plus moderator administrator suspended banned]"

# BEGIN HAS_MANYS
  has_many :posts
  has_many :assignments
  has_many :evaluations, class_name: "RSEvaluation", as: :source

  has_reputation :votes, source: {reputation: :votes, of: :posts}, aggregated_by: :sum

# BEGIN METHODS
  def voted_for?(post)
    evaluations.where(target_type: post.class, target_id: post.urlid).present?
  end

  # def has_role?(role_sym)
  #   roles.any? { |r| r.name.underscore.to_sym == role_sym }
  # end

end

1 个答案:

答案 0 :(得分:2)

使用rails 4更新用户帐户时遇到同样的问题,但是参数允许存在问题。

请更新您的控制器,如

class RegistrationsController < Devise::RegistrationsController
     before_filter :update_sanitized_params, if: :devise_controller?

     def update_sanitized_params
       devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:username, :email, :password, :password_confirmation, :firstname, :lastname, :displayname)}
       devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)}
     end

  end

它会起作用:))