【发布时间】: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' => 'contacts#create' -
控制器长这样:
-
控制器看起来像:
codedef 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