2016-05-17 117 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