# Passing aariable from controller to a different view?

**URL:** <https://www.ruby-forum.com/t/passing-aariable-from-controller-to-a-different-view/156188>\
**Category:** Rails\
**Created:** [December 22, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/passing-aariable-from-controller-to-a-different-view/156188 "2008-12-22T14:23:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 22, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/passing-aariable-from-controller-to-a-different-view/156188/1 "2008-12-22T14:23:43Z")

</div>

Hi Guys,

I am still very new to this rails stuff so I’m looking for some help. I  
have a login controller which authenticates a person and redirects them  
to the projects page, here is a snippet of code.

# ====================================================================== if request.post? person = Person.authenticate(params[:email\_address], params[:password]) if person session[“user\_id”] = person.id if person.type == “Customer” flash[:notice] = “You have successfully logged in” redirect\_to(:controller =\> “projects”, :action =\> “index” ) end else redirect\_to :action =\> “login” flash[:notice] = “Invalid user/password combination” end end

I have a projects controller which has a field company\_id (as a project  
belongs to a company). I also have a people table which holds the  
customers (as well as admins based on person.type).

I want to be able to redirect the person to the projects table and have  
the projects displayed only for that user.

In the customer model I have specified belongs\_to :company.  
In the project model I have also specified belongs\_to :company.  
In the company model I have specified has\_many :projects.

Somehow I need to get the person logging in from the login controller  
and transfer that to company controller and then pass that to the the  
projects controller to use in the project view.

But I have really stuck on how to do this!

Please can someone help!

Regards,

Dave

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 22, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/passing-aariable-from-controller-to-a-different-view/156188/2 "2008-12-22T14:54:18Z")

</div>

## Use restful authentication (or a similar plugin) for tracking that kind of stuff: [http://github.com/technoweenie/restful\_authentication](http://github.com/technoweenie/restful_authentication)

Ryan B.  
Freelancer

> **[frozenplague.net](http://frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [December 22, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/passing-aariable-from-controller-to-a-different-view/156188/3 "2008-12-22T15:49:45Z")

</div>

Ryan B. wrote:

> ## Use restful authentication (or a similar plugin) for tracking that kind of stuff: [http://github.com/technoweenie/restful\_authentication](http://github.com/technoweenie/restful_authentication)
> 
> Ryan B.  
> Freelancer  
> [http://frozenplague.net](http://frozenplague.net)

Couldnt I just do the following… in my projects controller?

before\_filter :find\_projects

def index

# @projects = Project.find(:all)

# @project\_company = @projects.company\_id

```
@projects = @results.find(:all)

respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @projects }
end

```

end

def find\_projects  
@user\_in = Person.find\_by\_id(session[“user\_id”])  
@company = Company.find\_by\_id(@user\_in.company\_id)  
@results = Company.find(@company.id).projects # Find all projects  
that belong to company  
end
