[Rails] Re: persisting an array from 1 action to another
by Mike other posts by this author
Aug 13 2007 3:45PM messages near this date
[Rails] Re: persisting an array from 1 action to another
|
[Rails] Save ActiveRecord attribute without type checking
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.
On Aug 13, 3:14 pm, "Vishnu Gopal" <g.vis...@[...].com> wrote:
> 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
>
> On 8/13/07, Mike <mikes...@[...].net> wrote:
>
>
>
> > 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?
>
> > On Aug 12, 6:54 pm, "Vishnu Gopal" <g.vis...@[...].com> wrote:
> > > 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
>
> > > On 8/13/07, Mike <mikes...@[...].net> wrote:
>
> > > > 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
>
> > > > On Aug 12, 5:48 pm, "Vishnu Gopal" <g.vis...@[...].com> wrote:
> > > > > 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
>
> > > > > On 8/13/07, Mike <mikes...@[...].net> wrote:
>
> > > > > > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Ta
lk" group.
To post to this group, send email to rubyonrails-talk@[...].com
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@[...].com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thread:
Mike
Vishnu Gopal
Bill Walton
Bill Walton
Bill Walton
Mike
Vishnu Gopal
Mike
Vishnu Gopal
Mike
|