Rails 控制器 - 创建后重定向到网站?

发布于 2024-09-24 01:03:59 字数 953 浏览 2 评论 0原文

我正在开发一个基本的应用程序,它有一个面向公众的表格(用于查询),任何人都可以填写并提交。然后将结果存储起来,以供公司执行他们想要的操作。

我已经确保,如果用户未登录,他们只能访问创建页面,但是一旦他们按预期提交表单,他们就会被带到登录页面,因为它试图向他们显示显示页面。

我当前的控制器如下:

# POST /enquiries
  # POST /enquiries.xml
  def create
    @enquiry = Enquiry.new(params[:enquiry])

    respond_to do |format|
      if @enquiry.save
        format.html { redirect_to(@enquiry, :notice => 'Enquiry was successfully created.') }
        format.xml  { render :xml => @enquiry, :status => :created, :location => @enquiry }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @enquiry.errors, :status => :unprocessable_entity }
      end
    end
  end

我想这行需要更改:

format.html { redirect_to(@enquiry, :notice => 'Enquiry was successfully created.') }

是否可以这样做:

format.html { redirect_to(http://www.google.com) }

I am working on a basic app that has a public facing form (for enquiries) that anyone can fill in and submit. The results are then stored for the company to do what they want with.

I have made sure that if the user is not logged in they can only access the create page, but once they submit the form, as expected, they are taken to the login page because its trying to show them the show page.

My current controller is as follows:

# POST /enquiries
  # POST /enquiries.xml
  def create
    @enquiry = Enquiry.new(params[:enquiry])

    respond_to do |format|
      if @enquiry.save
        format.html { redirect_to(@enquiry, :notice => 'Enquiry was successfully created.') }
        format.xml  { render :xml => @enquiry, :status => :created, :location => @enquiry }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @enquiry.errors, :status => :unprocessable_entity }
      end
    end
  end

I would imagine it's this line that needs to change:

format.html { redirect_to(@enquiry, :notice => 'Enquiry was successfully created.') }

Is it possible to do:

format.html { redirect_to(http://www.google.com) }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2024-10-01 01:03:59

是的,如果创建成功,您当然可以从控制器 redirect_to("http://any.url.com/you/want") 或执行您想要的任何其他操作。重定向到 show 操作只是一种常见模式。

但这真的是你想做的吗?如果您在提交表单后将用户重定向到外部网站,则您根本无法向他们提供有关刚刚发生的情况的任何反馈。用户可能会尝试再次提交查询,或者更糟糕的是,他们可能会认为出了问题,然后就忘记了它,失去了兴趣,等等。我强烈建议创建一个“感谢您的查询!”页面并重定向匿名用户。

Yes, you can certainly redirect_to("http://any.url.com/you/want") from your controller or do whatever else you want if create is successful. Redirecting to the show action is just a common pattern.

Is this really what you want to do, though? If you redirect your user to an outside website after submitting the form, you can't give them any feedback at all about what just happened. The user might try to submit the Enquiry again, or worse, they might think something went wrong and just forget about it, lose interest, etc. I'd strongly recommend creating a "Thanks for your enquiry!" page and redirecting anonymous users there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文