2016-10-10 15 views
-1

を受け、ApplicationMailerを使用して、私はこれが私のapplication_mailer.rb未定義のメソッドエラー

def request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others) 
    @contact_name = contact_name 
    @contact_hp = contact_hp 
    @contact_email = contact_email 
    @contact_postal = postal_code 
    @contact_level = contact_level 
    @contact_subject = contact_subject 
    @contact_lesson = contact_lessons 
    @contact_hours = contact_hours 
    @contact_others = contact_others 
    mail(to:'[email protected]', subject: 'Tutor Request') 
    end 

に、ビューが

def request_tutor 
     contact_name = params[:request_tutor][:contact_name] 
     contact_email = params[:request_tutor][:contact_email] 
     contact_hp = params[:request_tutor][:contact_hp] 
     contact_postal = params[:request_tutor][:postal_code] 
     contact_level = params[:request_tutor][:contact_level] 
     contact_subject = params[:request_tutor][:contact_subject] 
     contact_lesson = params[:request_tutor][:contact_lessons] 
     contact_hours = params[:request_tutor][:contact_hours] 
     contact_others = params[:request_tutor][:contact_others] 
     ApplicationMailer.request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others).deliver 
     flash[:success] = "We have received your request and will be in touch with you shortly!" 
     redirect_to root_path 
    end 

をレンダリングすることになっているそれぞれのコントローラで定義されている私は、同様の方法を設定していますアクションメーラでも、それぞれのコントローラーで私は name = params[:checkout][:your_name]を実行していて、エラー undefined method '[]' for nil:NilClassを返しません。私は何が欠けていますか?

development.log

Started GET "/" for 116.87.14.150 at 2016-10-10 23:02:37 +0000 
Cannot render console from 116.87.14.150! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
    [1m[36mActiveRecord::SchemaMigration Load (0.5ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m 
Processing by WelcomeController#index as HTML 
    Rendered welcome/index.html.erb within layouts/application (390.7ms) 
    Rendered layouts/_nav.html.erb (5.9ms) 
    Rendered layouts/_messages.html.erb (0.6ms) 
    Rendered layouts/_footer.html.erb (0.8ms) 
Completed 200 OK in 641ms (Views: 608.3ms | ActiveRecord: 0.0ms) 


Started GET "/welcome/request" for 116.87.14.150 at 2016-10-10 23:02:43 +0000 
Cannot render console from 116.87.14.150! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
Processing by WelcomeController#request_tutor as HTML 
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms) 

NoMethodError (undefined method `[]' for nil:NilClass): 
    app/controllers/welcome_controller.rb:16:in `request_tutor' 


    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.4ms) 
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms) 
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms) 
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (110.0ms) 
+0

こんにちはとスタックオーバーフローを歓迎します。これは、 'params [:request_tutor]'が空白(または 'params [:checkout]')の場合に起こります。あなたのサーバログ( 'log/development.log'またはあなたの端末ウィンドウ)を見て、そこにエラーメッセージがあるかどうかを見てください。それから近くの行を見てください。要求が送信されたときにサーバに送信された 'params'を表示します。' params'が期待通りに送信されていない可能性があります。 –

+0

興味深いのは、 'params [:checkout]'が動作することです。なぜ私は今なぜ動作していないのか不思議です。これは、guides.rubyonrails.orgのアプリケーションメーラセグメントに従って設定されました。私は両方のために同じ手順を踏んだが、一つは働いていて、一つはそうではない。 – angkiki

+0

クール... sooo ..私は質問したようにあなたのサーバーのログを見ることができます:)それは私たちが問題を特定するのに役立ちます(あなたが私たちに示したコードではないかもしれませんが、 –

答えて

0

OKからのエラー、あなたがフォームを設定の両方に同じアクションを使用しており、また、電子メールを作成します。これは問題が発生する場所です。フォームを最初にクリックすると、request_tutorアクションが実行されますが、まだフォームを送信していないため、params[:request_tutor]はありません。作成しようとする前に存在することを確認してください。

例えば、これは可能です:

def request_tutor 
    if params[:request_tutor].present? 
    contact_name = params[:request_tutor][:contact_name] 
    # ... lines skipped for brevity 
    # you still need the ones in your original action 
    ApplicationMailer.request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others).deliver 
    flash[:success] = "We have received your request and will be in touch with you shortly!" 
    redirect_to root_path 
    end 
    # If the code gets to here, there were no params, and the page 
    # will just render the template named the same as the action 
end 
+1

私を助けてくれてありがとう、ありがとう。 'params.present'を確認するのは私には起こらなかったと思います。 – angkiki

+0

いいえprobs :)我々はすべて、この点を別の点で学んだ:) –

関連する問題