StaticPagesController#contact 中的 ActionController::ParameterMissing (Ruby on Rails 6)

问题描述 投票:0回答:1

如何解决这个缺失的参数?

静态页面控制器:

class StaticPagesController < ApplicationController
  def home
  end

  def about
    @hero_title = 'about_us'
  end

  def services
    @hero_title = 'services'
  end
  
  def contact
    @hero_title = 'contact'
    
    @contact = Contact.new(contact_params)
    if @contact.save 
      # flash[:success] = "Message sent!"
      redirect_to contact_url(locale: I18n.locale)
    else
      render 'contact'
    end
  end

  def gallery
    @hero_title = 'gallery'
  end

  private

    def contact_params
      params.require(:contact).permit(:name, :email, :subject, :message)
    end
end

应用程序控制器:

class ApplicationController < ActionController::Base
  around_action :switch_locale

  def switch_locale(&action)
    locale = params[:locale] || I18n.default_locale
    I18n.with_locale(locale, &action)
  end
  
  def default_url_options
    { locale: I18n.locale }
  end
end

contact.html.erb:

<form action="<%= contact_path %>" accept-charset="UTF-8" method="post">
            <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
            <div class="row g-3">
              <div class="col-md-6">
                <div class="form-floating">
                  <input type="text" class="form-control" id="name" name="contact[name]" placeholder="Your Name">
                  <label for="name">Your Name</label>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-floating">
                  <input type="email" class="form-control" id="email" name="contact[email]" placeholder="Your Email">
                  <label for="email">Your Email</label>
                </div>
              </div>
              <div class="col-12">
                <div class="form-floating">
                  <input type="text" class="form-control" id="subject" name="contact[subject]" placeholder="Subject">
                  <label for="subject">Subject</label>
                </div>
              </div>
              <div class="col-12">
                <div class="form-floating">
                  <textarea class="form-control" placeholder="Leave a message here" id="message" name="contact[message]" style="height: 100px"></textarea>
                  <label for="message">Message</label>
                </div>
              </div>
              <div class="col-12">
                <button class="btn btn-primary w-100 py-3" type="submit">Send Message</button>
              </div>
            </div>
          </form>

路线:

Rails.application.routes.draw do
  scope "(:locale)", locale: /en|id/ do
    root 'static_pages#home'
    get  '/about',    to: 'static_pages#about'
    get  '/services', to: 'static_pages#services'
    get  '/contact',  to: 'static_pages#contact'
    post  '/contact',  to: 'static_pages#contact'
    get  '/gallery',  to: 'static_pages#gallery'
  end
end

错误: ActionController::ParameterMissing(参数丢失或值为空:联系):

app/controllers/static_pages_controller.rb:27:在

contact_params' app/controllers/static_pages_controller.rb:16:in 
contact' 应用程序/控制器/application_controller.rb:6:在`switch_locale'

def contact_params
    params.require(:contact).permit(:name, :email, :subject, :message)
end

请帮我解决这个错误

我希望联系表格不再出错

ruby-on-rails ruby-on-rails-6 contact-form
1个回答
0
投票

为什么你不尝试 Rails 方式:

<%= form_with(url: contact_path, method: "post", local: true, model: Contact.new) do |form| %>
  <div class="row g-3">
    <div class="col-md-6">
      <div class="form-floating">
        <%= form.text_field :name, class: "form-control", id: "name", placeholder: "Your Name" %>
        <%= form.label :name, "Your Name", class: "form-label", for: "name" %>
      </div>
    </div>
    <div class="col-md-6">
      <div class="form-floating">
        <%= form.email_field :email, class: "form-control", id: "email", placeholder: "Your Email" %>
        <%= form.label :email, "Your Email", class: "form-label", for: "email" %>
      </div>
    </div>
    <div class="col-12">
      <div class="form-floating">
        <%= form.text_field :subject, class: "form-control", id: "subject", placeholder: "Subject" %>
        <%= form.label :subject, "Subject", class: "form-label", for: "subject" %>
      </div>
    </div>
    <div class="col-12">
      <div class="form-floating">
        <%= form.text_area :message, class: "form-control", id: "message", placeholder: "Leave a message here", style: "height: 100px" %>
        <%= form.label :message, "Message", class: "form-label", for: "message" %>
      </div>
    </div>
    <div class="col-12">
      <%= form.submit "Send Message", class: "btn btn-primary w-100 py-3" %>
    </div>
  </div>
<% end %>

但请确保您有一个带有联系人的模型。您链接帖子并获取请求到同一路径是否有原因?

© www.soinside.com 2019 - 2024. All rights reserved.