I have this in my config/routes.rb
:
get '/:category/:region', to: 'categories#filtered_by_region'
The filtered_by_region
action is as shown below:
#filtered_by_region method
def filtered_by_region
@region = Region.where(title: params[:region]).first
@category = Category.where(title: params[:category]).first
@teams = Team.where(region_id: @region.id, category_id: @category.id)
end
I have a view filtered_by_region.html.erb
that looks as follows:
Region: <%= @region.title %>
Category: <%= @category.title %>
<% @teams.each do |team|%>
<%=team.title %>
<% end %>
region.rb
model is as follows:
class Region < ActiveRecord::Base
has_many :teams
attr_accessible :title
end
category.rb
model is as follows:
class Category < ActiveRecord::Base
has_many :teams
attr_accessible :title
end
team.rb
model is as shown below
class Team < ActiveRecord::Base
belongs_to :category
belongs_to :region
end
I also have the corresponding regions
, teams
and categories
tables already populated with data.
when i enter a url that looks like this:
http://localhost:3000/football/south_west
i get an error with the following message:
undefined method ``title' for nil:NilClass
I have realized both the @region
and @category
are returning nil but i do not understand why. i do have a category with football
title and a region with south_west
title in categories and regions tables respectively.