persisting an array from 1 action to another

If I have 2 actions in a controller, one is search, the other index:

def index end

def search        @items = Object.find(:all)        redirect_to action:=> :index end

How do I persist the @items array so that the view index.rhtml can iterator over it?

Thanks

There are two approaches. The one you choose depends on what you are doing.

1. Store it in session.

session[:items] = Object.find(:all)

Note, if the Object is an activerecord one, code like this stores a large amount of data in your session and it's definitely not recommended.

2. Call index instead of redirecting to it.

index render :action => :index

This doesn't do a browser redirect, but just calls index and then renders the index layout.

Vish

It didn't work. Here's what I have:

class HomeController < ApplicationController

  def index   end

  def search_by_name    @items = Object.find_by_name(params[:name])    index    render :action=> :index   end

end

What didn't work? What happened?

Try:

def index   logger.info "#{@items}" end

def search_by_name         @items = Object.find_by_name(params[:name])         index         render :action=> :index end

Does it get logged?

Vish

Ok, I finally got it to work. I had the params incorrect. It doesn't work with redirect_to but that's what I need. I don't want the URL to change like it does when using render.

In java, we could put request.setAttribute() to pass the attribute on the new request. There is nothing like that in Ruby?

Hi Mike, Mike wrote:

It doesn't work with redirect_to but that's what I need. I don't want the URL to change like it does when using render.

The URL displayed is controllable with routes. I suspect the reason your URL changed is that you invoked the index method, rather than just rendering with the index view.

In java, we could put request.setAttribute() to pass the attribute on the new request. There is nothing like that in Ruby?

You need to distinguish between Ruby and Rails. This is a Rails question. A Rails app lives for exactly one request-response cycle. A redirect_to ends one cycle and starts another. It sends a 302 back to the app as the end of the first cycle. You have a number of alternatives wrt passing an object on a redirect_to. Storing it in the session or storing it in the db and passing the id on the redirect_to are the two that come most quickly to mind.

HTH, Bill

Hi again Mike,

Sorry. I didn't see Vishnu's response before I answered.

Mike wroteL

  def index   end

  def search_by_name @items = Object.find_by_name(params[:name]) index render :action=> :index end

When you invoke the index action in search_by_name you're killing @items. Assuming your index.rhtml expects @items, just do the render. The view will use whatever instance variables you send it.

HTH, Bill

Bill Walton wrote:

> def search_by_name > @items = Object.find_by_name(params[:name]) > index > render :action=> :index > end

When you invoke the index action in search_by_name you're killing @items.

That's wrong. I shouldn't try to answer questions that late at night ;-p Sorry. Invoking the index action shouldn't, I don't think, clobber existing instance variables. Redirecting to the action would, but i think you can call it with harming them. I'll be doing a little sandbox exercise later today when i get a few to confirm. Will let you know if i find out anything different.

The other advice was correct though. You don't need to invoke the index action to use it's view.

Best regards, Bill

Mike,

URLs don't change when using render. They do when using redirect. Are you sure you're getting it right? I've used code fragments like that all over the place and it works for the question you posed initially. The Rails equivalent of the request-response cycle doesn't easily allow you to forward a request from one action to another, but you can emulate it by using the second sample code that I wrote above.

Also, in Java land, a lot of complex objects get serialized and stored into the session. This is frowned upon in the Rails world, session objects are tiny and usually simple datatypes.

Could I know what you're trying to do? Usually, there'll be a more 'Rails-like' way to do it.

Vish

Yes, it's quite simple (I believe). My controller (home) has an index action. The index view (index.rhtml) has a search form on it. I set it up so that the form action is search_by_name action in my home controller. The search_by_name action does the:

def search_by_name      @items = Object.find_by_name( params[:home][:name]      redirect_to index end

what I was attempting to do was to run a search, get the @items array then bring up the index page again with the form but this time display the results with a FOR loop.