Ruby on rails 3 设计-更改密码不起作用-而是注销用户
我正在制作一个自定义密码编辑表单,其中我只更改密码。以下是我的用户控制器代码:Ruby on rails 3 设计-更改密码不起作用-而是注销用户,ruby-on-rails-3,devise,Ruby On Rails 3,Devise,我正在制作一个自定义密码编辑表单,其中我只更改密码。以下是我的用户控制器代码: def change_my_password @user = User.find(current_user.id) end def update_my_password @user = User.find(current_user.id) #raise @user.inspect if @user.update_with_password(params[:user])
def change_my_password
@user = User.find(current_user.id)
end
def update_my_password
@user = User.find(current_user.id)
#raise @user.inspect
if @user.update_with_password(params[:user])
sign_in @user, :bypass => true
redirect_to users_path, :notice => "Password updated."
else
sign_in @user, :bypass => true
render action: "change_my_password", :alert => "Unable to update user."
end
end
这是我的用户模型
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable, :registerable,
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :password, :password_confirmation, :username, :name, :email, :as => :admin
attr_accessible :password, :password_confirmation, :username, :name, :email, :remember_me
#attr_protected :username, :name, :email, :remember_me, :password, :password_confirmation
validates_uniqueness_of :username
validates_presence_of :username, :email
validates_uniqueness_of :email
end
这一切似乎都很好,但发生的是-如果我输入了错误的密码确认,那么我会被提示输入错误,但是当我再次提交表单时,我已注销,密码不会更改。在我第一次提交表单时,它会从日志中为我注销,以使用错误的密码确认来更改密码。我不明白我哪里出错了-我甚至在用户中加入了sign_以避免必须注销,但它仍然不起作用。我在这里哪里会出错?使用
post
方法,而不是put
在路由中,并按如下方式查看-
路由.rb-
resources "users" do
collection do
get 'change_my_password'
post 'update_my_password'
end
end
<%= form_for(@user, :url => { :action => "update_my_password" }, :html => {:method => "post"}) do |f| %>
<%= f.text_field :password, :autocomplete => "off", :required => true %>
<%= f.text_field :password_confirmation, :required => true %>
<%= f.text_field :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
<%= f.submit 'Update', :class => 'btn-primary' %>
<%= link_to "Back", :back %>
<% end %>
更改我的密码.html.erb-
resources "users" do
collection do
get 'change_my_password'
post 'update_my_password'
end
end
<%= form_for(@user, :url => { :action => "update_my_password" }, :html => {:method => "post"}) do |f| %>
<%= f.text_field :password, :autocomplete => "off", :required => true %>
<%= f.text_field :password_confirmation, :required => true %>
<%= f.text_field :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
<%= f.submit 'Update', :class => 'btn-primary' %>
<%= link_to "Back", :back %>
<% end %>
{:action=>“更新我的密码”},:html=>{:method=>“post”})do | f |%>
“关闭”:必需=>true%>
正确%>
“我们需要您的当前密码来确认您的更改”,:required=>true%>
“btn主节点“%”
这对我来说没什么问题
干杯 不,它仍然不起作用。当我在错误消息后第二次尝试提交表单时,我收到一个错误,表明当前用户不再有效,即其已注销。哦,我明白问题所在。如果您为的
表单\u提供了错误的url,那么这就是问题所在。我更改了
url的表单,\u,效果令人惊讶:)请参阅上面我的更新答案。