Forum: Ruby on Rails undefined method `redirect_to'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9f821349470fc26abc6922b0816710c6?d=identicon&s=25 Mark Gallop (markg)
on 2006-05-31 05:25
Hi all

I have followed this tute:
http://hivelogic.com/articles/2005/12/01/ruby_rail...

to get rails running and all is fine except for getting:

undefined method `redirect_to'

when I load my test page.

I have the following in my xx.rhtml:

<%= select :group, :user_id, find_all_groups, {}, {:onclick =>
select_group, :size => find_all_groups.size + 1} %>

and in my xx_helper.rb I have:

  def select_group
    redirect_to "http://www.rubyonrails.org"
  end

I have installed actionpack.

Any help with the problem would be greatly appreciated.

Cheers,
Mark
05faf3e7b9ac65d5090ea404c27f8f26?d=identicon&s=25 Chen Ge (chenge)
on 2006-05-31 06:33
(Received via mailing list)
redirect_to is a method of controller, maybe you cannot write it in
helper
i guess.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-31 06:45
(Received via mailing list)
On 5/31/06, chenge <chenge2k@gmail.com> wrote:
>
>
>
> redirect_to is a method of controller, maybe you cannot write it in helper
> i guess.



There is another redirect_to but it's part of  the Javascript Generator
(RJS) not the general view.

I think if you include the following statement at the top of your
controller
it should do what you want.

helper_method :redirect_to
9f821349470fc26abc6922b0816710c6?d=identicon&s=25 Mark Gallop (markg)
on 2006-05-31 08:42
> There is another redirect_to but it's part of  the Javascript Generator
> (RJS) not the general view.
>
> I think if you include the following statement at the top of your
> controller
> it should do what you want.
>
> helper_method :redirect_to

Thanks. This worked.

Mark
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-31 08:44
Daniel ----- wrote:
> On 5/31/06, chenge <chenge2k@gmail.com> wrote:
>>
>>
>>
>> redirect_to is a method of controller, maybe you cannot write it in helper
>> i guess.
>
>
>
> There is another redirect_to but it's part of  the Javascript Generator
> (RJS) not the general view.
>
> I think if you include the following statement at the top of your
> controller
> it should do what you want.
>
> helper_method :redirect_to

The first solution will work, the second will not.

When called in a controller redirect_to takes the place of a render.
You can't do a redirect_to and a render.  redirect_to sends the browser
a basically empty response with the proper headers to tell the browser
to look somewhere else for the requested content.  So if the page is
already rendering this won;t work at all.

redirect_to from RJS will create the necesary javascript to change the
page you are viewing.  Most likely you want to do this form your
controller.  But if you really really want to do it from a helper called
while rendering a page, do this:

  def rjs_redirect_to(url)
    render(:update) do |page|
      page.redirect_to url
    end
  end

Then:

  <%= rjs_redirect_to 'http://rubyonrails.com' %>

Which would yield something like:

  <script type="text/javascript">
    location.href="http://rubyonrails.com";
  </script>
9f821349470fc26abc6922b0816710c6?d=identicon&s=25 Mark Gallop (markg)
on 2006-05-31 09:33
Alex Wayne wrote:
> When called in a controller redirect_to takes the place of a render.
> You can't do a redirect_to and a render.  redirect_to sends the browser
> a basically empty response with the proper headers to tell the browser
> to look somewhere else for the requested content.  So if the page is
> already rendering this won;t work at all.

Ahh, ok. Thanks.

> redirect_to from RJS will create the necesary javascript to change the
> page you are viewing.  Most likely you want to do this form your
> controller.  But if you really really want to do it from a helper called
> while rendering a page, do this:

I am new to rails/ruby so had no idea it was best to put it into the
controller.

A bit of background, I have a list of "groups" I want the user to choose
from. When the user clicks on a group I want to store the selected group
using sessions and then redirect the user to another page/controller.

>   def rjs_redirect_to(url)
>     render(:update) do |page|
>       page.redirect_to url
>     end
>   end
>
> Then:
>
>   <%= rjs_redirect_to 'http://rubyonrails.com' %>
>
> Which would yield something like:
>
>   <script type="text/javascript">
>     location.href="http://rubyonrails.com";
>   </script>

I have tried to use your code but I have not had much success. Using my
example, is this correct?

<%= select :group, :user_id, find_all_groups, {}, {:onclick =>
rjs_redirect_to('patient'), :size => find_all_groups.size + 1} %>

The code above results in an unrendered page displaying the html as
code.

Any hints would be great?

Cheers,
Mark
This topic is locked and can not be replied to.