2016-08-23 56 views
1

我不確定我對待它的方式是問題還是ERB。Rails 4 - 使用純文本和ERB的閃光警報

現在,當用戶註冊時,我發送了一封激活郵件。如果他們沒有激活,並嘗試重新登錄,系統會提示他們「對不起,您沒有授權。」我想修改它,以便讓他們重新發送電子郵件。

SessionsController

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 

內的用戶控制器I由 'resend_email' 功能。基本上只是創建一個的多數,所以有點多餘。

UsersController

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 

我試圖修改「信息」到幾個不同的版本,每一次我得到我得到這個語法從以往的經驗類似

/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 %> ...^

響應和有相似需求的人開採。

所以我不知道什麼是做到這一點的最好辦法,沒有寶石(我沒找到設計的功能,但我想學習如何做我自己)

我也讀到有時ERB和純文本不能很好地工作。但是,我也收到錯誤消息,只有<%= link_to "here", :controller => :user, :action => :resend_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 

用戶控制器

DEF resend_activation @user = User.find(PARAMS [:電子郵件]) @ user.send_activation_email 閃光[:信息] =「請檢查您的電子郵件以激活您的帳戶。」 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的不同用途,甚至還告訴它根據電子郵件參數重新搜索。

所以,我想更新我的路線

路線

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資源發現」。我正在研究何時使用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的標準#{}字符串插值,但實際上這並不能解決問題。

link_to只適用於瀏覽用默認情況下,雖然可以得到通過view_context對象訪問它:

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

順便說一句,可以在控制器使用ERB如果你自己編譯:

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字符串(如你用閃光燈在這裏做什麼),你顯示在視圖中的文本,您將需要添加一些自定義的方法,使HTML顯示爲真實的html:

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

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

這種方式僅文本some html將被顯示出來,而不是整個字符串<span>some html</span>

還有一個原因rawhtml_safe是必要的,這是因爲打印HTML有安全隱患和Rails的設計,使之更加困難。

假設您的用戶將其用戶名設置爲"<script>alert("hacked")</script>",並且該字符串以某種方式將其作爲真正的html進入頁面。你可能只是將你的用戶暴露給XSS(跨站腳本),你不想這樣做。因此,請確保當您使用raw <string>.html_safe時,您是而不是顯示用戶生成的任何內容。

+0

非常感謝你的所有信息,你帶領我一起追逐我,非常接近我。我不知道html_safe(或我發現相關的清理函數)。我現在更接近了,如果你可以看看我即將發佈的更新,我會非常感激。 – DNorthrup

0

爲什麼你的路線是告訴你,id = nil是因爲你的:activation_token是在該路線的id並沒有存儲在數據庫中的原因,幾乎與attr_accessor創建。相反,將:activation_token作爲列存儲在數據庫中。