2016-08-23 6 views
1

私がそれを扱っている方法が問題であるかERBであるかはわかりません。Rails 4 - プレーンテキストとERBによるフラッシュアラート

ユーザー登録時に、私はアクティベーションメールを送信しました。彼らが起動していない場合、ログインを試みると、「申し訳ありません、あなたは許可されていません」というメッセージが表示されます。私はそれを変更して電子メールを再送信できるようにしたい。

SessionsController私は 'resend_email' 関数を作ったユーザーコントローラ内の

class SessionsController < ApplicationController 
    def new 
    end 

    def create 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
     if user.activated? 
     log_in user 
     params[:session][:remember_me] == '1' ? remember(user) : forget(user) 
     redirect_back_or user 
     else 
     message = "Account not activated. " 
     message += "Check your email for the activation link, or click" + <%= link_to "here", :controller => :user, :action => :resend_email %>+ "to have it resent!" 
     flash[:warning] = message 
     redirect_to root_url 
     end 
    else 
     flash.now[:danger] = 'Invalid email/password combination' 
     render 'new' 
    end 
    end 

    def destroy 
    log_out if logged_in? 
    redirect_to root_url 
    end 

end 

。本質的には、作成するものの大部分だけですので、少し冗長です。

、私はいくつかの異なるバージョンへの「メッセージ」と私は以前の経験から、この構文を持っ

/home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:16: syntax error, unexpected '<' <%= link_to "here", :controlle...^/home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:16: syntax error, unexpected ',', expecting keyword_end ...o "here", :controller => :user, :action => :resend_email %> ...^/home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:16: syntax error, unexpected '>' ...r, :action => :resend_email %> ...^

のような応答を取得するたびに変更しようとした

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:index, :edit, :update, :destroy] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: :destroy 

    def index 
    @users = User.where(activated: true).paginate(page: params[:page]) 
    end 

    def show 
    @user = User.find(params[:id]) 
    redirect_to root_url and return unless @user.activated? 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     @user.send_activation_email 
     flash[:info] = "Please check your email to activate your account." 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

    def resend_email 
    @user.send_activation_email 
    flash[:info] = "Please check your email to activate your account." 
    redirect_to root_url 
    else 

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

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted" 
    redirect_to users_url 
    end 

    private 

    def user_params 
     params.require(:user).permit(:name, :email, :password, 
            :password_confirmation) 
    end 

    # Before filters 

    # Confirms a logged-in user. 
    def logged_in_user 
     unless logged_in? 
     store_location 
     flash[:danger] = "Please log in." 
     redirect_to login_url 
     end 
    end 

    # Confirms the correct user. 
    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 

end 

がUserController 、同様のニーズを持つ人が私のことを知っています。

だから私は(私は機能を考案見つけたが、私は自分自身でそれを行う方法を学習しようとしている)宝石せずに、これを行うための最善の方法何

を思ったんだけど、私はまたことを読みますERBとプレーンテキストがうまく動作しないことがあります。しかし、メッセージにもエラーが出るのは<%= link_to "here", :controller => :user, :action => :resend_email %>だけです。

他のコントローラが必要なのかどうか不安です。

EDIT INFO

問題と他の人のために元のコピーを残します。 以下の彼の答えで提供されている最大値を読んだ後、いくつか変更しました。 user.send_activation_email フラッシュ[@ :SessionsController

link = view_context.instance_exec do 
    ERB.new("<%= link_to 'here', :controller => :users, :action => :resend_activation %>").result(binding) 
end 
message = "Account not activated. " 
message += "Check your email for the activation." 
message += link # This is for demo purposes, just needed an output 

ユーザーコントローラ

デフresend_activation @user = User.find([メール]のparams):へ

変更情報] = "アカウントを有効にするためにメールを確認してください。" redirect_toのroot_url警告

<% flash.each do |message_type, message| %> 
    <%= content_tag(:div, sanitize(message), class: "alert alert-#{message_type}") %> 
    <% end %> 

この時点まで、私は今、私が期待したリンクを見ていて、私はそれをクリックしたときに、私の問題があるため

部分図 終了。

Couldn't find User with 'id'= - 私はUserという別の用途を試してみましたが、メールのパラメータに基づいて再検索してもらえました。

だから私は(私が思う)進歩を遂げ、そしてそれが今私を促し、 "何のGETリソースが見つからない"

ルート

Rails.application.routes.draw do 

    root 'static_pages#home' 
    get '/home', to: 'static_pages#home' 
    get '/help', to: 'static_pages#help' 
    get '/about', to: 'static_pages#about' 
    get '/contact', to: 'static_pages#contact' 
    get '/signup', to: 'users#new' 
    post '/signup', to: 'users#create' 
    get '/login', to: 'sessions#new' 
    post '/login', to: 'sessions#create' 
    delete '/logout', to: 'sessions#destroy' 
    resources :users 
    resources :account_activations, only: [:edit] 
    post '/resend_activation:email' => 'account_activations#resend_activation', 
             :constraints => { :email => /[^\/]+/ } 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 

に私のルートを更新してみました。私はgetとpostのどちらを使うべきかについての研究をしています。私は正しい方法に行ったと思っています。しかし、電子メールを送信した直後にそれを参照しようとしているのはなぜですか?それはroot_urlに戻ることになっていますか?

もう一度おねがいします。

更新#2

私はエラーがルートスイッチに追加すること、そして私のsessions_controller

def create 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
     if user.activated? 
     log_in user 
     params[:session][:remember_me] == '1' ? remember(user) : forget(user) 
     redirect_back_or user 
     else 
     message = "Account not activated. " 
     message += "Check your email for the activation." 
     message += " #{view_context.link_to "Resend Activation E-Mail", { action: "resend_activation", 
     controller: "account_activations", email: user.email }, method: :post}" 
     flash[:warning] = message 
     redirect_to root_url 
     end 
    else 
     flash.now[:danger] = 'Invalid email/password combination' 
     render 'new' 
    end 
    end 

を修正することにより停止してもらうことができました。しかし今は電子メールを外出されていませんすべてがログインに戻るだけです。

heroku logs --tail

はそれがメンバーを見つけ、それをレンダリングしますが、もはや郵送されていない私に

Herokuのログ

2016-08-24T09:44:48.990703+00:00 app[web.1]: I, [2016-08-24T09:44:48.990609 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Started GET "/resend_activation/[email protected]" for 100.15.65.126 at 2016-08-24 09:44:48 +0000 
2016-08-24T09:44:48.992317+00:00 app[web.1]: I, [2016-08-24T09:44:48.992217 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Processing by StaticPagesController#home as 
2016-08-24T09:44:48.992394+00:00 app[web.1]: I, [2016-08-24T09:44:48.992349 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Parameters: {"email"=>"[email protected]"} 
2016-08-24T09:44:48.997712+00:00 app[web.1]: I, [2016-08-24T09:44:48.997648 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendering static_pages/home.html.erb within layouts/application 
2016-08-24T09:44:48.999032+00:00 app[web.1]: I, [2016-08-24T09:44:48.998965 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered static_pages/home.html.erb within layouts/application (1.1ms) 
2016-08-24T09:44:49.010260+00:00 app[web.1]: I, [2016-08-24T09:44:49.010186 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered layouts/_shim.html.erb (0.4ms) 
2016-08-24T09:44:49.010516+00:00 app[web.1]: I, [2016-08-24T09:44:49.010461 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered layouts/_shim.html.erb (0.0ms) 
2016-08-24T09:44:49.010642+00:00 app[web.1]: I, [2016-08-24T09:44:49.010591 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered layouts/_headElement.html.erb (7.6ms) 
2016-08-24T09:44:49.020206+00:00 app[web.1]: D, [2016-08-24T09:44:49.020136 #5] DEBUG -- : [8cfcee3c-133c-489e-8877-523578821d67] User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 103], ["LIMIT", 1]] 
2016-08-24T09:44:49.020630+00:00 app[web.1]: I, [2016-08-24T09:44:49.020565 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered layouts/_header.html.erb (3.8ms) 
2016-08-24T09:44:49.025024+00:00 app[web.1]: I, [2016-08-24T09:44:49.024957 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Rendered layouts/_footer.html.erb (0.7ms) 
2016-08-24T09:44:49.025337+00:00 app[web.1]: I, [2016-08-24T09:44:49.025273 #5] INFO -- : [8cfcee3c-133c-489e-8877-523578821d67] Completed 200 OK in 33ms (Views: 26.1ms | ActiveRecord: 1.8ms) 

を指示します。私はそれが仕事になるために確立しなければならなかったルートを得るためだと思いますか?次のコードで

+0

これを解決しましたか? –

+0

以下の回答を参照してください –

答えて

2

message += 
     "..."" + 
     <%= link_to "here", :controller => :user, :action => :resend_email %> + 
     "..." 

あなたは、通常のルビーにERBタグを使用しようとしている - 動作しないこと。 ERBはテンプレートでのみ使用できます。

通常、Rubyの標準#{}文字列補間を使用することをお勧めしますが、実際には問題は解決しません。あなたはそれを自分でコンパイルした場合、コントローラにERBを使用することが可能、ちなみに

link = "#{view_context.link_to 'here', :controller => :user, :action => :resend_email}" 

:あなたはview_contextオブジェクトを経由して、それへのアクセスを得ることができますが

link_toは、デフォルトでビューにのみ使用可能です

link = ERB.new("<%= view_context.link_to(...) %>").result(binding) 

ERBで使用可能な変数/メソッドは、別のコンテキストで呼び出すことによって変更できます。:

link = view_context.instance_exec do 
    ERB.new("<%= link_to(...) %>").result(binding) 
end 

これも標準#{}文字列補間で動作します:

link = view_context.instance_exec do 
    "#{link_to(...)}" 
end 

それは言及する価値があること(あなたはフラッシュでここでやっているように)あなたがコントローラでカスタムHTML文字列を作成する場合は、とき

# in controller 
flash[:test] = "<span>some html</span>" 

# in view 
<%= raw flash[:test].html_safe %> 

この方法でのみテキスト:あなたは本当のhtmlとしてHTML表示をするためにいくつかのカスタムメソッドを追加する必要がありますビューにテキストを表示します3210は表示され、全体ではなく、文字列<span>some html</span>

rawhtml_safeが必要な理由がありますが、とhtmlを印刷することは、セキュリティ上のリスクがあり、Railsのは、それがより困難にするために設計されているからですられます。

ユーザがユーザ名を"<script>alert("hacked")</script>"と設定し、この文字列が何らかの形で真のhtmlとしてページに表示されているとします。ユーザーはXSS(クロスサイトスクリプティング)に晒されたばかりですが、これはやりたくないことです。したがって、raw <string>.html_safeを使用するときは、ではなく、がユーザー生成のものを表示していることを確認してください。

+0

あなたのすべての情報をありがとう、私は非常に近い私を持っているガチョウの追跡に私を導いた。私はhtml_safe(または私が関連すると思った機能をサニタイズする)を知らなかった。私はかなり近づいています。私が掲載しようとしているアップデートを見ていただければ幸いです。 – DNorthrup

0

あなたのルートがあなたの:activation_tokenは、そのルートでidあり、それはデータベースに格納されていないためid = nilがあることを語っている理由は、それが事実上attr_accessorで作成されます。代わりに、:activation_tokenをデータベースに格納します。

関連する問題