Dependancies
I had added in basic scaffolding for a Project, and then I wanted to connect 2 other controllers to Project - Sites, and Assets. The goal here is when a client adds or selects a new Project that project needs to be linked to Links and Assets.
In this Rails project - Project is basically the main organization system for the system. Each client can have multiple projects and those projects organize items such as Assets, and Sites. The Asset controller contains items such as Logos, colors, Fonts, pictures, etc. The Sites controller lets clients state what they like and don’t like about a particular site. This helps show what a client is thinking in a website.
So overall we needed to create a series of dependancies.
The migration
First I created a migration to link Sites, and Assets with the Projects. I needed to add a column to the sites and assets tables called :project_id. After I set it up I ran the migration.
class AddDependanceToProject < ActiveRecord::Migration
def self.up add_column :sites, :project_id, :integer add_column :assets, :project_id, :integer end
def self.down remove_column :sites, :project_id remove_column :assets, :project_id end end
The Models
Next I setup the belongs_to and has_many relationships. Under the models I setup the following:
For Sites and Assets: class Site < ActiveRecord::Base belongs_to :project end
For projects: class Project < ActiveRecord::Base has_many :sites has_many :assets end
In the project
Lastly we need to connect the relationship -
In my haml application template right after I have my flash notice I added a link to show off which project I am running:
%p= session:pname if !session:pname.nil?
In the projects_controller I needed to have after a project was either created or opened for the project to be stored in memory. So I added to the show object: session:pname = @project.title
to the create object: def create @project = Project.new(params:project)
respond_to do |format|
if @project.save
session[:project_id] = @project.id
flash[:notice] = 'Project was successfully created.'
session[:pname] = @project.title
format.html { redirect_to(@project) }
format.xml { render :xml => @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
In the assets_controller I needed connect the project with the assets that has been uploaded under create: @asset.project_id = session:project_id
Next on my list
Next on my list is to connect the Links controller, to the Projects controller, and set it up so that when you physically go into a project you can add new content in the form of Assets and Links. All in all it is a work in progress.