ruby-on-rails - Rails 4 form_for 标签将表单操作留空

标签 ruby-on-rails ruby forms form-for

我在 Rails 中制作一个基本表单,出于某种原因,当我制作表单时,操作是空白的,因此我收到路由错误“没有路由匹配 [POST]”/contacts/new”

查看代码如下:

<%= form_for  :contact,  :url => {:action => "create"} , html: { class: "contact-form"} do |f| %>

  <div class="form-group col-md-12 col-sm-12 col-xs-12">
    <h4>Choose your option below</h4>
     <%= f.label :option, class: 'sr-only' %>
     <%= f.select :option, options_for_select(["I would like to reserve a place for myself", "I would like Better Place for ashes I currently possess", "I would like Better Place for ashes currently at a funeral home", "I operate a funeral home and would like to know more"]), {}, multiple: false, class: "form-control" %><br />
  </div>
  <div>
    <h4>Please provide us with some details</h4>
  </div>

  <div class="col-md-6 col-sm-6 col-xs-12 form-group">
    <%= f.label :name, class: 'sr-only' %>
    <%= f.text_field :name, placeholder: "Your name", class: 'form-control', style: "height: 40px;" %><br />
  </div>

  <div class="col-md-6 col-sm-6 col-xs-12 form-group">
    <%= f.label :email, class: 'sr-only' %>
    <%= f.text_field :email, placeholder: "Your email", class: 'form-control', style: "height: 40px;" %><br />
  </div>

  <div class="col-md-6 col-sm-6 col-xs-12 form-group">
    <%= f.label :phone, class: 'sr-only' %>
    <%= f.text_field :phone, placeholder: "Your phone", class: 'form-control', style: "height: 40px;" %><br />
  </div>

  <div class="col-md-12 col-sm-12 col-xs-12 form-group">
    <%= f.label :comments, class: 'sr-only' %>
    <%= f.text_area :comments, placeholder: "Enter any comments or questions here", class: "form-control",:rows => "6" %>
  </div>

  <div class="col-md-12 col-sm-12 col-xs-12 form-group">
    <%= f.submit 'Submit Details', class: 'btn btn-block btn-cta btn-cta-primary'  %>
  </div>  
<% end %>

这是生成的 html 格式:

<form id="contact-form" class="contact-form" method="post" action=""> 

当我运行 rake routes 时,会出现正确的路线 (contacts_path):

     pages_home GET  /pages/home(.:format)    pages#home
         root GET  /                        pages#home
  pages_about GET  /pages/about(.:format)   pages#about
pages_options GET  /pages/options(.:format) pages#options
    pages_faq GET  /pages/faq(.:format)     pages#faq
     contacts POST /contacts(.:format)      contacts#create
  new_contact GET  /contacts/new(.:format)  contacts#new

当我提交表单时,它试图 POST 到新操作而不是创建操作,所以我得到:没有路由匹配 [POST] "/contacts/new"

我的问题是,当存在适当的路由时,为什么表单操作留空,我该如何解决?

谢谢!

编辑以在下方显示 Controller :

    class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    @option = params[:contact][:option]
    @name = params[:contact][:name]
    @email = params[:contact][:email]
    @phone = params[:contact][:phone]
    @comments = params[:contact][:comments]

    if @contact.save
      redirect_to new_contact_path, :notice => "Thanks for contacting us! We'll be in touch shortly."
    else
        render new_contact_path
        flash[:notice] = "Oops. Please enter the correct information and try again."
    end
  end

  private

  def contact_params
    params.require(:contact).permit(:option, :name, :phone, :email, :comments)
  end

end

最佳答案

您应该按以下方式使用 form_for:

form_for @contact, url: contacts_path, html: { class: "contact-form"} do |f|
  #                ^^^^^^^^^^^^^^^^^^ this is optional, see @nathanvda's comment

其中 @contact 是在您的 Controller 操作中设置的变量,如下所示:

@contact = Contact.new
# you can eventually pre-fill some attributes here:
# @contact = Contact.new(user_id: current_user.id, important: false)

这很棘手:form_for 正在接受一个 URL 助手来知道在哪里提交表单,在您的例子中是 /contacts。你会说“但它是索引操作的地址!”我会回答“是的,但是 form_for 将请求用作 POST 而不是 GET)

关于ruby-on-rails - Rails 4 form_for 标签将表单操作留空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29234813/

相关文章:

ruby - 如何正确卸载Ruby 1.9.1

ruby-on-rails - 完全卸载 Rails

ruby-on-rails - AWS bundle 安装错误 : Make sure that `gem install memcached -v ' 1. 8. 0'` 在 bundle 之前成功

ruby-on-rails - Rails 4 - simple_form 和来自 url 的预填充字段

ruby - 无法在 Ubuntu 中成功安装带有 Ruby 支持的 Vim

ruby-on-rails - 未定义的方法错误

forms - 在提交事件中使用 Google App 脚本获取 Google 表单中特定问题/项目的值

javascript - 恢复旧的提交函数javascript

javascript - 在 JavaScript 中引用操作字段

ruby-on-rails - Ruby on Rails : Submitting an array in a form