2016-05-17 116 views
1

我在我的rails應用程序中出現錯誤。 參數是丟失或爲空值:接觸Param缺少錯誤 - 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。

+0

u能張貼服務器日誌錯誤 – 7urkm3n

+0

Server日誌已經被添加到原來的職位。 – Andy

+0

在前面的控制檯中,您應該看到發送給服務器的參數,請將其放在此處。 – ppascualv

回答

1

只需將您的形式參數爲空,原因從那裏得到錯誤的。另外,你必須定義你的路線。

路線

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

你有teamviewer嗎? – Andy

+0

對不起,我只是更新了它,我沒用它。檢查我最後一次更新......在'routes'部分編輯。 – 7urkm3n

+0

我現在很困惑。 – Andy

-1

,因爲你在這裏嘗試分配PARAMS你得到這個錯誤:

@contact = Contact.new(contact_params) 

,自目前尚無提交,您指定params.require(:contact)你的錯誤。

這裏就是你要做的:

:作爲一個良好的軌道公民的顯示形式(通常是 new),另一個用於提交它(通常 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

可以使用一種方法在提交時顯示新表單和處理請求,但與新/創建的分離更加清晰。

+0

爲什麼'contact_params'?和哪裏? – 7urkm3n

+0

在你的控制器中,檢查我更新的答案 – meta

+0

不起作用,我仍然得到相同的錯誤信息 – Andy

1

您在呈現表單時未定義@contact

呈現形式的行動需要類似像@contact = Contact.new

另外一個代碼,你需要定義一個POST方法提交表單。聯繫方式應該是您的發佈方法,其中表單已提交,並且您需要使用我以前提供的代碼進行其他操作GET。

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