【问题标题】:Devise custom password changing设计自定义密码更改
【发布时间】:2012-06-22 04:16:36
【问题描述】:

正如标题所述,我正在使用设计。我遇到了这个 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

为了分离动作和视图,我的 config/routes.rb 是这样的: match '/password' =&gt; 'users#password', :as =&gt; 'password'

当我单击表单中的“更改密码”按钮时出现问题。 这是它带给我的链接:“http://localhost:3000/assets?action=update_password&controller=users”,我收到错误“找不到具有 id=assets 的用户”

我不知道它为什么以这种方式行事以及我做错了什么,因为我复制了网页上的内容。有没有人有这方面的经验并且可以帮助我?谢谢!

【问题讨论】:

    标签: ruby-on-rails passwords


    【解决方案1】:

    如果您使用的是 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=&gt;[:en], :formats=&gt;[:html], :handlers=&gt;[:erb, :builder, :arb, :coffee]}.。出于某种原因,它需要一个我不想要的视图。在 update_with_password 之后似乎没有重定向。
    • 实际上我能够解决这个问题。现在的问题在于,每次都不管输入什么。我收到错误“密码不能为空,密码确认不匹配”,即使它们确实匹配
    • 这个&lt;%= password_field_tag :password %&gt; 应该是&lt;%= f.password_field :password %&gt; 否则它不会被限定在用户的参数中。如果您在进行更改之前检查 html,我想您会明白我的意思。
    猜你喜欢
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多