How to redirect_to index action after update

388 views
Skip to first unread message

goodieboy

unread,
Dec 30, 2007, 4:34:49 PM12/30/07
to resource_controller
Hi,

I'm doing an update on the index page. There is a little form for each
of the items in the list. I'd like to be able to do an update on a
give item, and then have the controller redirect to the SAME index
url. How is this possible with r_c?

Thanks and thank you for such an awesome project!

Matt

James Golick

unread,
Dec 30, 2007, 5:25:14 PM12/30/07
to resource_...@googlegroups.com
Hi Matt,

I'm not totally clear on what you're asking. If you are simply asking
how to change the redirect from the update action, try this:

update.wants.html { redirect_to collection_url }

Otherwise, I'll need some more details.

goodieboy

unread,
Dec 30, 2007, 6:30:06 PM12/30/07
to resource_controller
Hi James,

I've got a list of products. The list has a little text fields for
entering the position number. What I want to have happen is to have
the form call update for the given product, and after the update,
display the index page again. I actually got it working by doing this:

def update
unless params[:product][:new_position].nil?
Product.find(params[:id]).new_position=(params[:product]
[:new_position])
respond_to do |format|
format.html {redirect_to collection_url}
end
else
super
end
end

Is there a cleaner way to do this? :)

Thanks,
Matt

On Dec 30, 5:25 pm, "James Golick" <jamesgol...@gmail.com> wrote:
> Hi Matt,
>
> I'm not totally clear on what you're asking. If you are simply asking
> how to change the redirect from the update action, try this:
>
> update.wants.html { redirect_to collection_url }
>
> Otherwise, I'll need some more details.
>

James Golick

unread,
Dec 30, 2007, 6:36:46 PM12/30/07
to resource_...@googlegroups.com
So, the update is performed differently, depending on the params
present? If it's a totally different update, I think what you've done
is the clearest method.

goodieboy

unread,
Jan 2, 2008, 5:11:09 PM1/2/08
to resource_controller
Hi James,

The update is performed differently yes, but doesn't have to be. I'm
just doing a standard update, but on only one attribute. So, what I'd
really like to do is have a_r handled everything, but then just handle
the response and redirect to the index page instead. Is there a way to
do that?

Thanks,
Matt

On Dec 30 2007, 6:36 pm, "James Golick" <jamesgol...@gmail.com> wrote:
> So, the update is performed differently, depending on the params
> present? If it's a totally different update, I think what you've done
> is the clearest method.
>

James Golick

unread,
Jan 3, 2008, 11:31:19 AM1/3/08
to resource_...@googlegroups.com
Yes, I think that's a much better approach.

In that case:

  update.wants.html { redirect_to collection_url }

goodieboy

unread,
Jan 3, 2008, 8:29:17 PM1/3/08
to resource_controller
I see, but the problem I've had is that I need to check for the
presence of a certain request parameter. But I can't access the params
from within the block. Do you know how I could access the params
array?

If I could just do this:

unless params[:new_position].nil?
update.wants.html { redirect_to collection_url }
end

I'd be set! Any ideas on that?

Thanks,
Matt

On Jan 3, 11:31 am, "James Golick" <jamesgol...@gmail.com> wrote:
> Yes, I think that's a much better approach.
>
> In that case:
>
> update.wants.html { redirect_to collection_url }
>

James Golick

unread,
Jan 3, 2008, 9:23:58 PM1/3/08
to resource_...@googlegroups.com
You can't access the params from inside of the block? I don't seem to have any problems doing

update.wants.html {
  unless params[:something].nil?
   redirect_to collection_url
  end
}

Which version of rails/r_c are you on?

goodieboy

unread,
Jan 4, 2008, 9:30:33 AM1/4/08
to resource_controller
Cool, I ended up doing this:

update.wants.html {
redirect_to params[:id] ? collection_url : object_url
}

And it works great. Thank you!

Matt

On Jan 3, 9:23 pm, "James Golick" <jamesgol...@gmail.com> wrote:
> You can't access the params from inside of the block? I don't seem to have
> any problems doing
>
> update.wants.html {
> unless params[:something].nil?
> redirect_to collection_url
> end
>
> }
>
> Which version of rails/r_c are you on?
>
Reply all
Reply to author
Forward
0 new messages