Rails - 电子邮件控制器重定向混乱

时间:2012-03-16 19:28:25

标签: ruby-on-rails redirect referer

我有一个处理联系电子邮件的控制器。我有这样的行动:

def representative
    @contact = UserContact.new()
    if request.method == 'POST'
     @contact = UserContact.new(params[:merchant_contact])
     if @contact.valid?
      @contact.contact_email = current_user.email
      @contact.contact_phone = current_user.phone_number
      Emailer.representative_contact(@contact, representative).deliver # sends the email
          redirect_to **????**, :flash => { :notice => "Thank you! Someone will get back to you shortly."}
     else
      render :representative
     end
    else
     render :representative
    end
end

我从我的代码中的不同位置调用 contact_representative_path ,我希望在提交重定向到用户点击 contact_representative_path 的位置之后。我尝试过:返回,但它只是呈现代表性视图。

1 个答案:

答案 0 :(得分:0)

redirect_to和return不会结束请求,为了结束请求,您需要在最后添加return。

尝试使用redirect_to contact_representative_path,:flash => { :notice => "Thank you! Someone will get back to you shortly."} and return

更新

在会话哈希中,您可以存储可在多个请求中使用的请求网址。  session[:return_to] ||= request.referer 然后拨打redirect_to session[:return_to], :flash => { :notice => "Thank you! Someone will get back to you shortly."}

检查this question,希望这可以解决您的问题。