2017-10-18 16 views
0

オブジェクト全体とその属性を変数に戻したいのですが、電子メールのみを返しているようです。params、railsとして電子メールを使用してfind_byからユーザオブジェクトを返す必要があります

がUserController

.... 
def resend_act_create 
    @user = User.find_by(email: params[:user][:email]) 
    if @user 
    @user.send_activation_email 
    flash[:info] = "Please check your email to activate your account." 
    redirect_to root_url 
    else 
    flash[:warning] = "Could not find email in database" 
    render 'account_activations/new' 
    end 
end 
... 
ビュー/ usermailer/account_activations.html.erb

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"account_activations", :email=>"[email protected]", :id=>nil} missing required keys: [:id]): 
6: Welcome to the Sample App! Click on the link below to activate your account: 
7: </p> 
8: 
9: <%= link_to "Activate", edit_account_activation_url(@user.activation_token, 
10:              email: @user.email) %> 

サーバーエラー私は私のコントローラのアクションを構築する方法を確認していないので、私はルートのIDを含むことができ、 ?

EDIT User.rbとルート

class User < ApplicationRecord 

.... 
def send_activation_email 
    UserMailer.account_activation(self).deliver_now 
end 
.... 

routes.rbを

.... 
resources :account_activations, only: [:edit] 

UserMailer.rbを追加するには、ファイル内の行9で

def account_activation(user) 
    @user = user 
    mail to: user.email, subject: "Account activation" 
end 
+1

'routes.rb'を確認すると、' account_activations#edit'はおそらく存在しません。 –

+0

経路が終了する最初のアカウントアクティベーションメールが送信されていますが、これは再送信です。だからあなたの言う 'find_by'は' find'メソッドのようにUserオブジェクトを返さなければなりません? find_byはコンソールで正常に動作します。 –

+1

ルートとユーザーモデルには何が含まれていますか? –

答えて

2

views/usermailer/account_activations.html.erb

引数としてidを追加してください:

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email, id: @user.id) %> 

編集:

なぜ私はこれが必要なのですか?

あなたがRailsの魔法の使用しているのであなたは、それを必要とする:この行は、あなたのためのルートを生成resources :account_activations, only: [:edit] を。これらのルートには、パラメータとして:idが必要です。 http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing に移動した場合例の生成ルートを確認できます(実際のルートを確認するには、端末にbin/rake routesを実行することもできます)。
:editルートには:idパラメータが必要です。
:resourcesを使用せずに自分でルートを作成した場合は、:id以外のパラメータを使用してレコードを識別できます。

+0

メールが送られてきているのですが、私はなぜこの行が必要なのか聞いてもいいですか?find_byはそれをインクルードする必要があるためです。例えば '@user = User.new(user_params ) 'は、アクティベーションを作成して送信するときに使用しますか? –

+1

私はあなたのためにより多くの情報を追加しました。答えを受け入れるとマークしてください;) –

+0

あなたがIDを必要としない場合は、最初のアカウントアクティベーションメールを送信するときに、ユーザーが作成されたときに、どこかにある必要があります。 paramsはクエリー文字列として渡されるので、私は両方を確認して確認します。 –

関連する問題