2016-04-25 4 views
0

私はいくつかのチュートリアルを完了しました。ユーザーにメッセージを表示する方法はさまざまです。メッセージをユーザーにプッシュする方法と一貫している必要がありますか?

を例えば、コントローラに私は、このアクションがあります。

def edit 
    user = User.find_by(email: params[:email]) 
    if user && !user.activated? && user.authenticated?(:activation, params[:id]) 
     user.activate 
     log_in user 
     flash[:success] = "Account activated! Welcome to foodiker!" 
     redirect_to root_url 
    else 
     flash[:danger] = "Invalid activation link." 
     redirect_to root_url 
    end 
    end 

をそして時々私はこれを持っているコントローラで:

def create 
    @recipe = Recipe.new(recipe_params) 

    respond_to do |format| 
     if @recipe.save 
     current_user.recipes << @recipe 
     format.html { redirect_to myrecipes_url, notice: 'Recipe was successfully created.' } 
     format.json { render :show, status: :created, location: @recipe } 
     else 
     format.html { render :new } 
     format.json { render json: @recipe.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

私はまた、レスポンダの宝石のようなものについて読んだし、それを使ってrespond_withむしろrespond_to。

これらを異なる形式でプッシュしても問題ないのですか?ベストプラクティスが欠けていますか?

答えて

0

flashまたはnoticeメッセージをredirect_toメソッドに指定すると、対応するキーのメッセージはflashに設定されます。

基本的に、これらは等価です。

redirect_to myrecipes_url, notice: 'Recipe was successfully created.' 

# The same as if you did this: 
flash[:notice] = 'Recipe was successfully created.' 
redirect_to myrecipes_url 

これは、メソッドのドキュメントのthe examples sectionに記載されています。

関連する問題