Topic: How to pass model object in redirect_to()???

Hi,
  I am trying to pass a model object in the redirect_to but all I get is nil object.

Here's my sample code

class LoginController < ApplicationController

   @user_obj = Person.new(params[:person])
    @param_person = Person.where("email_address=? and password = ?", @user_obj.email_address, @user_obj.password)
     redirect_to index_path(@param_person => params[@param_person])
end
----------------------------------------------------------------------------------------------------
class MainController < ApplicationController
  def index
   puts '******************* INSIDE INDEX ***********'
    puts params[@param_person].inspect
    @show_person = Person.new(params[@param_person]) 
  end
 
end


When I try an print the params in MainController I get NIL.  What am I doing wrong. I don't want to use render, because I want my url changed as well hence I have to use redirect_to.

Re: How to pass model object in redirect_to()???

Firstly NEVER use puts statements use logger.debug
Store the id of the person object in the session hash then use that to find the person record you need in the action you are redirecting to.

class LoginController < ApplicationController

   @user_obj = Person.new(params[:person])
    @param_person = Person.where("email_address=? and password = ?", @user_obj.email_address, @user_obj.password)
    session[:person_id] = @param_person.id
     redirect_to index_path(@param_person => params[@param_person])
end

Then in your main controller

class MainController < ApplicationController
  def index
    #puts params[@param_person].inspect #You NEVER need this anyway, just look in your log file if you want to see what params are being passed into this action
    @param_person = Person.find(session[:person_id])
    @show_person = Person.new(params[@param_person]) #you can't mass assign an id attribute so you really need to be very carefull with what you are doing here. Anyone could pass anything they wanted by just amending the form data in their broswer
  end
 
end
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to pass model object in redirect_to()???

Thanks James. !

I figured that it was not possible to pass the object between controllers without using session. I tried passing objects in flash but found that you can only pass strings.

The reason I was trying to avoid session was that I had to make a second call to the database to pull out the customer, which I was trying to avoid.

Re: How to pass model object in redirect_to()???

you can pass objects as params as in a redirect_to but you have to make sure that the route and the action that you are redirecting to will accept those params in the first place. you could always write a method in your application controller called something like current_person that will cache the result in an instance variable then call the current_person method whenever you need it

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to pass model object in redirect_to()???

Is session variables a clean way of programming?