【问题标题】:Rails 4 form_for tag is leaving the form action blankRails 4 form_for 标签将表单操作留空
【发布时间】:2015-03-24 14:09:26
【问题描述】:

我在 Rails 中制作一个基本表单,由于某种原因,当我制作表单时,操作为空白,因此我收到路由错误“No route matches [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] "/contacts/new"

我的问题是,当存在适当的路线时,为什么表单操作会留空,我可以做些什么来解决它?

谢谢!

编辑显示控制器如下:

    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

【问题讨论】:

  • #new#create 的控制器是什么样的?
  • 另外,当您查看表单时,地址栏中的地址是什么?我认为这可能是由于非标准路线造成的,因此 Rails 做出了不正确的假设。您可以尝试将POST 路由设置为post 'new_contact' =&gt; 'contacts#create'
  • 控制器长这样:
  • 控制器看起来像:code 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] @cmets = params[:contact][:cmets] if @contact.save redirect_to new_contact_path, :notice => "感谢您与我们联系!我们会尽快与您联系。" " else render new_contact_path flash[:notice] = "糟糕。请输入正确的信息,然后重试。"结束结束
  • 不要将那么大的代码放在 cmets 中。用上面的代码以可读的格式编辑你的答案。

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


【解决方案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 正在接受一个 URL 帮助程序来知道在哪里提交表单,在你的情况下是 /contacts。你会说“但它是索引操作的地址!”我会回答“是的,但form_for 使用的请求是POST 而不是GET)

【讨论】:

  • 如果使用实例变量,则无需再指定url。
  • @nathanvda 谢谢,在这种情况下不需要指定 url 但是如果它是命名空间的呢? (以/settings/ 为例,我其实不知道它是否可行)
  • 命名空间,例如在admin 下你会写form_for [:admin, @contact] do |f|
  • 即使我编辑了你所拥有的 form_for 并设置了 @contact 变量(编辑后显示控制器),当 Rails 指南说时,我仍然在表单上得到一个空白操作呈现表单时,操作应为 action="contacts/create"。
【解决方案2】:

恕我直言,标准/简单的解决方案是用以下方式编写它:

<%= 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| %>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多