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.
Mark G. (Guest)
on 2006-05-31 07: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
Chen G. (Guest)
on 2006-05-31 08:33
(Received via mailing list)
redirect_to is a method of controller, maybe you cannot write it in
helper
i guess.
Daniel -. (Guest)
on 2006-05-31 08:45
(Received via mailing list)
On 5/31/06, chenge <removed_email_address@domain.invalid> 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
Mark G. (Guest)
on 2006-05-31 10: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
Alex W. (Guest)
on 2006-05-31 10:44
Daniel ----- wrote:
> On 5/31/06, chenge <removed_email_address@domain.invalid> 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>
Mark G. (Guest)
on 2006-05-31 11:33
Alex W. 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.