Conditional rendering of menu items.

2 views
Skip to first unread message

Carson

unread,
Apr 25, 2008, 12:13:50 PM4/25/08
to Beginning Ruby on Rails E-Commerce
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">
&nbsp;&nbsp;Choose a Department
</div>
<div class="department_textbox">
<%= render :partial => "departments" %>
</div>
<!-- conditional test for deparatment selected?????????? --
>
<div class="category_header">
&nbsp;&nbsp;Choose a Category
</div>
<div class="category_textbox">
????????????????????????
</div>
<!-- end conditional test -->

Any suggestions?

Carson

Ed Campbell

unread,
Apr 26, 2008, 5:08:55 PM4/26/08
to Beginning Ruby on Rails E-Commerce
Hey guys:
 
The solution came to me over the weekend:
 
 
Carson

Reply all
Reply to author
Forward
0 new messages