Rails 4 form_for标签将表单操作留空

时间:2015-03-24 14:09:26

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

我在rails中创建了一个基本表单,由于某种原因,当我创建表单时,操作是空白的,因此我收到路由错误"没有路由匹配[POST]" / contacts /新"

以下是视图代码:

<%= 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路线时,会出现正确的路线(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]&#34; / contacts / new&#34;

我的问题是为什么当适当的路线存在时,表格动作会留空,我该怎么做才能修复它?

谢谢!

编辑以显示以下控制器:

    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

2 个答案:

答案 0 :(得分:1)

您应该使用form_for,如下所示:

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

其中@contact是控制器动作中设置的变量,如下所示:

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

这很棘手:form_for正在接受一个网址帮助,以便知道在哪种情况下提交表单/contacts。你会说“但它是索引动作的地址!”我会回答“是的,但form_for使用的请求为POST而不是GET

答案 1 :(得分:0)

标准/简易解决方案imho是用以下方式编写的:

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

在您的new操作中,设置

@contact = Contact.new

使用符号也可以,但我认为您必须按如下方式编写:

<%= form_for :contact, :url => contacts_path, method: :post, html: { class: "contact-form"} do |f| %>

但是使用实例变量是首选方式。