2

私は RoR を初めて使用し、Hartlのチュートリアルを進めてきました (これはすばらしいものでした)。私は第 9 章まで順調にフォローアップしました (私の最終的な目標はマイクロポスト サイトを作成することではないため、少し調整しています)。その時点で、「remember me」チェック ボックスを追加してパスワードをリセットする機能をアプリに追加することにしたので、Railscast チュートリアルに戻りました(Hartl の提案による)。チェック ボックスは非常にスムーズに進みましたが、パスワードのリセット セクションでレンガの壁にぶつかりました。次から次へとエラーが続いています。私は自分自身を助けることができず、少し微調整できなかったことを認めなければなりません - 私はform_for構文の代わりに構文を使用しようとしましたform_tag。メールアドレスを送信できるところまで来ましたが、No route matches [POST] "/reset_password/new"メッセージ。過去 2 日間、stackoverflow で同様の投稿を読んで提案を試してみましたが、うまくいくものを思いつくことができないようです。助けてください!

要点は次のとおりです。

私のパスワード リセット ビューは次の場所にあります/app/views/reset_password/new.html.erb

<% provide(:title, 'Reset Password') %>
<h1>Reset Password</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for @user, url: new_reset_password_path do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.submit "Reset Password", class: "btn btn-large btn-methyl" %>
    <% end %>
  </div>
</div>

私のコントローラーは次の場所にあり/app/controllers/reset_password_controller.rbます:

class ResetPasswordController < ApplicationController

  def new
    @user = User.new
  end

  def show
  end

  def create
    @user = User.find_by_email(params[:email].downcase)
    user.send_password_reset if user
    redirect_to root_path, notice: "Email sent with password reset instructions."
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.reset_password_sent_at < 2.hours.ago
      redirect_to_new password_reset_path, alert: "Reset password request has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to root_path, notice: "Password has been reset!"
    else
      render :edit
    end
  end
end

私のルートは次の場所にあり/config/routes.rbます:

Methylme::Application.routes.draw do

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :reset_password

  root to: 'static_pages#home'

  match '/signup', to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
.
.
.
end

最後に$ rake routes、次のように報告します。

               users GET    /users(.:format)                   users#index
                     POST   /users(.:format)                   users#create
            new_user GET    /users/new(.:format)               users#new
           edit_user GET    /users/:id/edit(.:format)          users#edit
                user GET    /users/:id(.:format)               users#show
                     PUT    /users/:id(.:format)               users#update
                     DELETE /users/:id(.:format)               users#destroy
            sessions POST   /sessions(.:format)                sessions#create
         new_session GET    /sessions/new(.:format)            sessions#new
             session DELETE /sessions/:id(.:format)            sessions#destroy
reset_password_index GET    /reset_password(.:format)          reset_password#index
                     POST   /reset_password(.:format)          reset_password#create
  new_reset_password GET    /reset_password/new(.:format)      reset_password#new
 edit_reset_password GET    /reset_password/:id/edit(.:format) reset_password#edit
      reset_password GET    /reset_password/:id(.:format)      reset_password#show
                     PUT    /reset_password/:id(.:format)      reset_password#update
                     DELETE /reset_password/:id(.:format)      reset_password#destroy
                root        /                                  static_pages#home
              signup        /signup(.:format)                  users#new
              signin        /signin(.:format)                  sessions#new
             signout DELETE /signout(.:format)                 sessions#destroy
                help        /help(.:format)                    static_pages#help
               about        /about(.:format)                   static_pages#about
             contact        /contact(.:format)                 static_pages#contact

よろしくお願いします。

4

2 に答える 2

1

パスワード リセット ビューで ( ) にリンクするのではなく、パスワード リセット メールを送信する ( )にリンクするnew_reset_password_path必要があると思います。newreset_password_pathcreate

ルートが期待どおりに動作しない場合 (たとえば、createルートに関連付けられたxxx_path名前がない場合)、単純に個別に宣言する必要があります。

post '/reset_password', to: 'reset_password#create', as: 'send_reset_password' # for example
...
于 2013-03-17T19:18:50.333 に答える
0

これは Ryan による最高の認証チュートリアルの 1 つです。

http://railscasts.com/episodes/250-authentication-from-scratch-revised

于 2013-03-17T23:15:24.850 に答える