设计自定义密码更改
问题描述:
正如标题所述,我正在使用设计。我遇到了这个描述这样做的过程的这个 link。 我有这个用户控制器:设计自定义密码更改
def update_password
@user = User.find(current_user.id)
#raise @user.to_yaml
if @user.update_attributes(params[:user])
# Sign in the user by passing validation in case his password changed
sign_in @user, :bypass => true
#flash[:notice] = "Password Successfully Changed"
redirect_to settings_path
else
flash[:notice] = @user.errors.full_messages
render password_path
end
end
忽略#raise @user.to_yaml
。 而这样的观点:
<h1>Change Password</h1>
<%= form_for(:user, :url => { :action => 'update_password'}) do |f| %>
<div>
<%= f.label :current_password %>
<%= f.password_field :current_password %>
</div>
<div>
<%= f.label :password %>
<%= password_field_tag :password %>
</div>
<div>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div>
<%= f.submit "Change Password" %>
</div>
<% end %>
我有映射到该控制器的方法这一观点:
def password
@user = current_user
end
为了分离动作和图,我的配置/ routes.rb中是这样的: match '/password' => 'users#password', :as => 'password'
当我单击窗体中的“更改密码”按钮时出现问题。 这是它给我带来的链接:“http:// localhost:3000/assets?action = update_password & controller = users”,我得到错误“找不到id = assets的用户”
I不知道为什么它以这种方式行事,以及我做错了什么,因为我已经复制了网页所说的内容。有没有人有这方面的经验,可以帮助我?谢谢!
答
如果您使用Rails 3或更新你可以设置使用资源的语法您的路线:
resources :users do
member do # These routes will apply to specific model instances
get 'password' # /users/:id/password
put 'update_password' # /users/:id/updated_password
end
end
那么你就可以在你的form_for
声明中使用的路径帮手
<%= form_for @user, :url => update_password_user_path(@user) do |form| %>
...
<% end %>
谢谢对于帮助,但现在当我点击提交按钮时,我被':Missing template/password with'{:locale => [:en],:formats => [:html],:handlers => [:erb, :builder,:arb,:coffee]}。'。出于某种原因,它需要一个我不想要的视图。它似乎不是在update_with_password之后重定向。 – Vasseurth
其实我能解决这个问题。现在的问题在于每一次,无论它是什么输入。我收到错误“密码不能为空,密码确认不匹配”,即使它们匹配 – Vasseurth
这个“'应该是''否则它不会被限制在用户的params中。如果你在做改变之前检查html,我想你会明白我的意思。 – patrickmcgraw