0
我在我的网站上有一个联系表单。它工作正常,下面的代码:将错误消息从一个控制器传递到另一个控制器
class ContactsController < ApplicationController
def new
@contact = Contact.new(:id => 1)
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
redirect_to('/', :notice => "Message sent ....")
else
flash.now[:error] = "Oops, something went wrong. Please try again"
render 'new'
end
end
end
#contact
= simple_form_for @contact, :url=>{:controller=>:contacts, :action=>:create}, :html=>{:method=>:post} do |f|
= render 'shared/error_messages', :target => @contact
= f.input :name
= f.input :email
= f.input :subject
= f.input :body, :as => :text, :input_html => { :rows => 5 }
= f.submit "Send"
我已经决定,我想在pages/index.html.haml
嵌入的联系方式在我的主页所以我坚持的形式部分,链接到它的我的主页底部,并添加一个实例变量到我的页面控制器中的索引操作。如果我填写正确的表格,电子邮件会发送,一切都很好。
但是,如果表单提交失败,那么我会更喜欢contacts_controller中的new操作:我更喜欢pages_controller的index操作。我知道我可以使用redirect_to "/"
重定向到此操作,但当我这样做时,我没有在窗体上看到error_messages。
如何将控件从contacts_controller转移到pages_controller并在失败的表单提交中包含错误消息?
感谢很多:)这工作得很好, – stephenmurdoch 2011-01-06 07:04:58