The web site I'm working on has a "Department" menu rendering all
department names in the Department table. Upon department selection, I
want to display an additional "Categories" menu rendering all category
names in the Categories table tagged with the selected department
number. My controller statements look like this:
def index
@products = Product.find_all_by_on_catalog_promotion(true)
@departments = Department.find(:all)
end
def categories
@categories = Category.find_all_by_department_id(params[:id])
end
My migrations look like this:
class CreateDepartments < ActiveRecord::Migration
def self.up
create_table :departments do |t|
t.integer :department_id
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :departments
end
end
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.integer :department_id
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :categories
end
end
My views look like this:
<% for department in @departments %>
<div class="department_name">
<%= link_to
department.name, :action => "categories", :id =>
department.department_id %>
</div>
<br />
<% end %>
<% for category in @categories %>
<div class="category_name">
<%= link_to
category.name, :action =>
"list_products_by_category", :id => category %>
</div>
<br />
<% end %>
Now, it seems to me, in the application layout I need to wrap a render
statement in a conditional statement testing whether a category has
been selected (or something). However, it doesn't come to me how I can
do that.
The application looks like this:
<div class="department_header">
Choose a Department
</div>
<div class="department_textbox">
<%= render :partial => "departments" %>
</div>
<!-- conditional test for deparatment selected?????????? --
>
<div class="category_header">
Choose a Category
</div>
<div class="category_textbox">
????????????????????????
</div>
<!-- end conditional test -->
Any suggestions?
Carson