Undefined method `redirect_to'


#1

Hi all

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

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


#2

redirect_to is a method of controller, maybe you cannot write it in
helper
i guess.


#3

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


#4

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


#5

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:


#6

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:

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