def create
@purchase = Purchase.new(params[:purchase])
if session[:purchase_id] != @purchase.save
redirect_to(@purchase, :notice => "Thank you. You good lookin person you.")
end
end
我正在尝试
A -重定向到URL,或其他控制器路径,或
B -刷新他们订购的表单(new_purchases_path)并flash:通知用户他们的购买成功了。当我尝试添加一个Url(这将是一个感谢消息页)我得到语法错误。此代码将用户重定向到索引列表(用户无法访问)
如果我取出def create
,默认情况下它会闪烁一个通知并显示他们完成的表单。我正在使用simple_form,我不确定如何覆盖这些默认值。有什么建议至少重定向到一个url吗?
示例:
A -重定向到URL或其他控制器路径
redirect_to :action => "show", :id => 5
redirect_to "http://www.rubyonrails.org"
redirect_to new_purchases_path
redirect_to purchases_url
redirect_to :back
B -刷新他们订购的表单(new_purchases_path)
render :action => "new"
编辑:这里有一个flash消息的通用示例:
if @foo.save
redirect_to foos_path, :notice => "Foo saved"
else
flash[:notice] = "Some errors occured"
render :action => "new"
end
这是我今天创建的一个create方法的例子
def create
@book = Book.new(params[:book])
if @book.save
redirect_to searchbook_path, notice: 'Book was successfully saved'
else
render :action => 'results'
end
结束你可以试试这个
def create
@purchase = Purchase.new(params[:purchase])
if @purchase.save
redirect_to purchase_path, :notice 'Thanks for your purchase'
else
render :action => 'new'
end
end
这是假设你有一个购买和新的路径…不过如果你能告诉我们你得到了什么错误就更好了