Ruby-on-rails 我可以在 RoR 的视图中使用 redirect_to 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3170475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:50:59  来源:igfitidea点击:

Can I use redirect_to in a view in RoR?

ruby-on-rails

提问by ben

In my app, I've got a little box that appears on every page, checking on the status of requests made by the user. If a request is accepted at any time, then the user should automatically be taken to a certain page. This is my code so far:

在我的应用程序中,每个页面上都会出现一个小框,用于检查用户发出的请求的状态。如果请求在任何时候被接受,那么用户应该自动被带到某个页面。到目前为止,这是我的代码:

 <% offersMade.each do |w| %>
  <% if w.accepted == true %>
   <% redirect_to offer_path(:email => "[email protected]") %>
  <% end %>
 <% end %>

But I'm getting this error:

但我收到此错误:

undefined method `redirect_to' for #<ActionView::Base:0x1042a9770>

Is it not possible to user redirect_to in a view? If not, is there something else I can use? Thanks for reading.

不能在视图中使用 redirect_to 吗?如果没有,还有什么我可以使用的吗?谢谢阅读。

回答by Salil

redirect_tois a method of ActionController::Base Class so you can not use it in ActionView.

redirect_to是 ActionController::Base Class 的一个方法,所以你不能在 ActionView 中使用它。

You can try following

您可以尝试以下

<% if w.accepted == true  %>
  <script type="text/javascript">
    window.location.href="/logins/sign_up"  // put your correct path in a string here
  </script>
<% end %>

Edited for the email parameter

针对电子邮件参数进行了编辑

window.location.href="/logins/sign_up?email=<%= w.email %>"

Sorry i don't know if there is anything in ruby for that.

抱歉,我不知道 ruby​​ 中是否有任何内容。

回答by errakeshpd

If you want to use redirect_to from view , do this method:

如果要从 view 使用 redirect_to ,请执行以下方法:

syntax :<%controller.redirect_to path %>

语法:<%controller.redirect_to 路径 %>

Example:<% controller.redirect_to users_profile_path %>

示例:<% controller.redirect_to users_profile_path %>

回答by Mitch

The code in the view could be moved into the controller which would make redirect_to available.

视图中的代码可以移动到控制器中,这将使 redirect_to 可用。

class ApplicationController < ActionController::Base
  before_action :check_for_accepted_offer

  def check_for_accepted_offer
    if Offer.any? { |o| o.accepted }
      redirect_to offer_path(:email => "[email protected]")
    end
  end
end

If the OP wanted to immediately change the URL when an offer is accepted then none of the Ruby code shown in the answers would help because it is only evaluated when the page is loaded. The OP would need to setup some kind of polling or push strategy to alert the browser when an offer has been accepted and then use the JavaScript redirect scheme posted in another answer:

如果 OP 在接受报价时想要立即更改 URL,那么答案中显示的 Ruby 代码都没有帮助,因为它仅在页面加载时进行评估。OP 需要设置某种轮询或推送策略以在接受报价时提醒浏览器,然后使用另一个答案中发布的 JavaScript 重定向方案:

window.location.href="/logins/sign_up?email=<%= w.email %>"

Although, this does not answer the question: "How can I use redirect_to in a view", I think this answer would ultimately have been more useful to the OP. I stumbled across someone using this answer to redirect to another page, when the redirect should have been performed in the controller.

虽然,这并没有回答这个问题:“我如何在视图中使用 redirect_to”,但我认为这个答案最终对 OP 更有用。当重定向应该在控制器中执行时,我偶然发现有人使用这个答案重定向到另一个页面。

回答by Suman Mukherjee

redirect_tois not a method of ActionView. Its a method of ActionController. You can probably use Javascript window.location.href on page load or some other event to take your user to another page.

redirect_to不是 的方法ActionView。它的一种方法ActionController。您可能可以在页面加载或其他一些事件中使用 Javascript window.location.href 将您的用户带到另一个页面。

回答by scottwb

Yes, you can call controller.redirect_tofrom your view to get what you want without having to render the whole response and then use javascript on the client to make a new request.

是的,您可以controller.redirect_to从您的视图中调用以获得您想要的内容,而无需呈现整个响应,然后在客户端上使用 javascript 来发出新请求。

In your example, this would look like:

在您的示例中,这看起来像:

<% offersMade.each do |w| %>
  <% if w.accepted == true %>
    <% controller.redirect_to offer_path(:email => "[email protected]") %>
  <% end %>
<% end %>

Note that this controller.redirect_towill not break out of your loop, so you will probably want to break, and you'll probably want to make sure that the rest of your view is only conditionally rendered if you didn't redirect.

请注意,这controller.redirect_to不会跳出您的循环,因此您可能想要break,并且您可能想要确保只有在没有重定向的情况下才会有条件地呈现视图的其余部分。

(Disclaimer: I don't necessarily condone this technique. You would be better off doing this in your controller or helper, as others have mentioned.)

(免责声明:我不一定容忍这种技术。正如其他人提到的那样,您最好在控制器或助手中执行此操作。)