Can I use redirect_to in a view in RoR?

2020-03-01 04:01发布

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@gmail.com") %>
  <% 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.

5条回答
家丑人穷心不美
2楼-- · 2020-03-01 04:37

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

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@gmail.com")
    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:

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.

查看更多
何必那么认真
3楼-- · 2020-03-01 04:39

redirect_to is a method of ActionController::Base Class so you can not use it in 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.

查看更多
来,给爷笑一个
4楼-- · 2020-03-01 04:47

redirect_to is 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.

查看更多
Juvenile、少年°
5楼-- · 2020-03-01 04:49

Yes, you can call controller.redirect_to from 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.

In your example, this would look like:

<% offersMade.each do |w| %>
  <% if w.accepted == true %>
    <% controller.redirect_to offer_path(:email => "email@gmail.com") %>
  <% end %>
<% end %>

Note that this controller.redirect_to will 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.

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

查看更多
看我几分像从前
6楼-- · 2020-03-01 04:57

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

syntax : <%controller.redirect_to path %>

Example:<% controller.redirect_to users_profile_path %>

查看更多
登录 后发表回答