【问题标题】:Param missing error - Ruby on Rails参数丢失错误 - Ruby on Rails
【发布时间】:2016-05-18 03:55:36
【问题描述】:

我的 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。

【问题讨论】:

  • 你可以发布服务器日志错误
  • 服务器日志已添加到原帖中。
  • 在此行之前的控制台中,您应该看到发送到服务器的参数,请将其放在这里。
  • 请求后添加所有内容。
  • 您在显示表单之前设置了@contact 对象吗?如您所见,您有simple_form_for @contact,它需要一个对象@contact,因此在显示您的联系页面的操作中,您必须设置@contact = Contact.new

标签: ruby-on-rails ruby


【解决方案1】:

您在呈现表单时没有定义@contact

呈现表单的操作需要类似@contact = Contact.new的代码

另一方面,您需要定义一个 POST 方法来提交表单。 Contact 应该是您提交表单的 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

【讨论】:

    【解决方案2】:

    只是你的表单参数是空的,从那里得到错误的原因。此外,您必须定义路线。

    表格

    <%= simple_form_for Contact.new, url: :contact_emailer do |f| %>
    

    路线

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

    【讨论】:

    • 你有teamviewer吗?
    • 对不起,我刚刚更新了它,我没有使用它。检查我的最后更新...在routes 部分编辑。
    • 我现在很困惑。
    • 我有一个问题要问你,你的contact 方法位于哪个控制器中?
    • 它位于 home_controller.rb,但由于阅读了这里的所有 cmets,我已将其移出到 contact_controller.rb。
    【解决方案3】:

    您收到此错误是因为您尝试在此处分配参数:

    @contact = Contact.new(contact_params)
    

    由于尚未提交任何内容,并且您指定了params.require(:contact),因此您会收到错误消息。

    这就是你要做的:作为一个优秀的 Rails 公民,创建一个单独的方法来显示表单(通常是 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

    可以使用一种方法在提交时同时显示新表单和处理请求,但新/创建的分离更加清晰。

    【讨论】:

    • 为什么是contact_params ?在哪里?
    • 你能粘贴生成的 HTML 表单吗(从你的浏览器)?
    • 老兄,为什么是contact_params 而不是params
    • 由于我的错误,表单没有生成。对不起。但我已经发布了表单的 html 代码。
    • 哦,我以为这个错误信息是在提交表单之后。一秒。
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2016-02-13
    • 2016-11-10
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多