views:

38

answers:

2

Hi,

I have a small question regarding rails. I have a search controller which searches for a name in database, if found shows the details about it or else I am redirecting to the new name page. Is there anyway after redirection that the name searched for to automatically appear in the new form page?

Thanks in advance.

+1  A: 

You can use the ActionController::Flash to pass data between actions.

def search(searchedName)
  # perform search on DB

  flash[:searchedName] = searchedName

  redirect_to new_name
end

def new_name
end

new_name.html.erb:

<% if flash[:searchedName] %>
  <%= text_field_tag flash[:searchedName] %>
<% end %>
Anero
Nice. But how do I set this in that particular text field in the next page?
Sainath Mallidi
@Cyborgo: added snippet for view to set text box content with passed data
Anero
thanks a lot for the help!! :)
Sainath Mallidi
+1  A: 

Well, let's say you are saving this 'name' entered by user on a variable named @name.
So, on the controller that do the action of search, you did something like:

if @name
  ...#action if the name was found
else
  session[:name] = @name
  ...#here is the action you did to redirect

On the declaration of the method called (you said it was 'new') :

def new
  @name = session[:name] if session[:name]  #I don't know exactly if the condition is this one, 
                                             #but you have to confirm that the session was instatiated
  session.delete(:name) #to don't let garbage
  ...#continuous the action of show the new page
     #and on the page you have access to a variable @name, that have the name searched before.
Gabriel L. Oliveira
I believe it's just `if session[:name]`
j.