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.
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?
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.
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.
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.