0

ユーザーがサインアップすると(つまり、新しいユーザーが作成されると)、チュートリアルにリダイレクトされるようにしようとしています。ただし、ユーザーがサインアップすると、ユーザー名と電子メールは一意である必要があるというエラー メッセージが表示され (一意であっても)、「新しい」ページが再度表示されます。

代わりにリダイレクトすると、これは正常に機能し@userます。

これは私のコントローラーです:

  def create
    @user = User.new(params[:user])
    respond_to do |format|
       if @user.save
         login(@user)
         format.html { redirect_to "static/tutorial", success: 'Congratulations on starting your journey!' }
         format.json { render json: @user, status: :created, location: @user }
       else
         format.html { render action: "new" }
         format.json { render json: @user.errors, status: :unprocessable_entity }
       end
     end
  end

そして、これらは User.rb の関連する行です:

validates_confirmation_of :plain_password
validates_presence_of :name, :username, :email
validates_presence_of :plain_password, :on => :create
validates_uniqueness_of :email, :username
4

2 に答える 2

0

それを見るとかなり怖い

validates_presence_of :plain_password, :on => :create

暗号化されていないパスワードをデータベースに保存していますか?

respond_toあなたの問題に関して、あなたは/の使用を検討したいかもしれませんrespond_with

class UsersController < ApplicationController
  respond_to :html, :json

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      # set sessions
    end

    respond_with @user, location: 'static/tutorial'
  end
end

このブログ記事を読んでくださいhttp://bendyworks.com/geekville/tutorials/2012/6/respond-with-an-explanation

于 2013-06-28T07:26:19.353 に答える