2016-05-17 9 views
1

私のレールアプリケーションでエラーが発生しています。 paramが存在しないか、値が空である:接触Param missing error - Ruby on Rails

ホームコントローラー

def contact 
    @contact = Contact.new(contact_params) 

     if @contact.save 
     redirect_to root_path 
     firstname = params[:contact][:firstname] 
     lastname = params[:contact][:lastname] 
     email = params[:contact][:email] 
     message = params[:contact][:message] 
     ContactMailer.contact_email(firstname, lastname, email, message).deliver 
     flash[:success] = "Thanks for your message, we will be in touch soon." 
     else 
     redirect_to home_contact_path 
     flash[:danger] = "Opps, there was a problem! Please fill out all the fields." 
     end 
    end 

    private 

    def contact_params 
     params.require(:contact).permit(:firstname, :lastname, :email, :message) 
    end 

メーラー/フォーム

<div class="contact-form"> 
    <%= simple_form_for @contact do |f| %> 
    <%= f.input :firstname, required: true %> 
    <%= f.input :lastname, required: true %> 
    <%= f.input :email, required: true %> 
    <%= f.input :message, required: true %> 
    <%= f.submit "Get in touch!", class: "btn btn-info" %> 
    <% end %> 
</div> 

問い合わせページ

<!DOCTYPE html> 

<div class="jumbotron"> 
    <h1 class="center-aligned">Contact Me</h1> 
</div> 

<div class="main-content"> 
    <p class="center-aligned">You can contact me via the social media links at the bottom of the site or via the contact form below.</p> 

    <%= render 'home/mailer' %> 
</div> 

Contact.rb

class Contact < ActiveRecord::Base 
    validates_presence_of :firstname, :lastname, :email, :message 
end 

サーバーログ

Started GET "/home/contact" for 127.0.0.1 at 2016-05-17 14:13:13 -0500 
Processing by HomeController#contact as HTML 
Completed 500 in 6ms (ActiveRecord: 0.0ms) 

ActionController::ParameterMissing (param is missing or the value is empty: contact): 
    app/controllers/home_controller.rb:28:in `contact_params' 
    app/controllers/home_controller.rb:9:in `contact' 

Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (21.9ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (9.8ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.5ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (80.8ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.9ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.3ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.8ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.9ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (72.9ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (1.3ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (2.0ms) 
    Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (135.5ms) 

あなたが何かを必要とする場合は私に知らせてください。私はまだRuby on Railsを学んでいます。あなたがここのparamsを割り当てようとするので

+0

Uは、サーバーログエラー – 7urkm3n

+0

Serverのログを投稿することができ、元のポストに追加されました。 – Andy

+0

コンソールにprevioslyこの行には、サーバーに送信するparamsが表示されます、ここに入れてください。 – ppascualv

答えて

1

単にあなたのフォームパラメータがそこからエラーを取得しての空、理由です。また、ルートを定義する必要があります。

フォーム

ルート

post 'contact_emailer' => "contacts#contact", as: :contact_emailer 
+0

チームビューアはありますか? – Andy

+0

申し訳ありません、私はちょうどそれを更新し、私はそれを使用しないでください。最後の更新を確認してください... 'routes'部分で編集しました。 – 7urkm3n

+0

私は今とても混乱しています。 – Andy

-1

あなたはこのエラーを取得する:

@contact = Contact.new(contact_params) 

、以来、何もまだ提出されていない、とあなたはparams.require(:contact)あなたがエラーを取得することを指定しました。

:それはこのように少し見える良いレールの市民としての形を示すために(通常は new)を別のメソッドを作成し、それを提出するための別の1(通常は create

:ここ

は、あなたが何をすべきかです

def new 
     @contact = Contact.new # this is empty model, no data yet 
    end 

    def create 
    @contact = Contact.new(contact_params) 
    # ... and all the other fancy stuff you have there 
    end 

ビューのファイル名をnew.html.erbに更新するか、コントローラのrenderに電話するのを忘れないでください。

送信時に新しいフォームとハンドル要求の両方を表示することはできますが、new/createとの分離はより明確です。

+0

なぜ' contact_params '?そして、どこ ? – 7urkm3n

+0

あなたのコントローラに私の更新された回答を確認 – meta

+0

まだ動作しません、私はまだ同じエラーメッセージが表示されます – Andy

1

フォームをレンダリングするときに@contactを定義していません。フォームを送信するために

フォームをレンダリングするアクションが一方@contact = Contact.new

のようなコードを必要とする、あなたは、POSTメソッドを定義する必要があります。連絡先は、フォームが提出されたポストメソッドでなければなりません。また、私が前もって与えたコードで別のアクションを取得する必要があります。

def contact 
    @contact = Contact.new(contact_params) 
end 

def create 
    if @contact.save 
     redirect_to root_path 
     firstname = params[:contact][:firstname] 
     lastname = params[:contact][:lastname] 
     email = params[:contact][:email] 
     message = params[:contact][:message] 
     ContactMailer.contact_email(firstname, lastname, email, message).deliver 
     flash[:success] = "Thanks for your message, we will be in touch soon." 
     else 
     redirect_to home_contact_path 
     flash[:danger] = "Opps, there was a problem! Please fill out all the fields." 
     end 
    end 

routes.rbを

resources :contacts