Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use redirect_to in a view in RoR?

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.

like image 581
ben Avatar asked Jul 03 '10 05:07

ben


People also ask

What is the difference between render and redirect_to?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

How do I redirect in Rails?

Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.


2 Answers

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.

like image 158
Salil Avatar answered Oct 21 '22 11:10

Salil


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

syntax : <%controller.redirect_to path %>

Example:<% controller.redirect_to users_profile_path %>

like image 41
errakeshpd Avatar answered Oct 21 '22 11:10

errakeshpd