1

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.

Optimus Pette
  • 3,250
  • 3
  • 29
  • 50
  • please specify file and line number where the error came from – MikeZ Mar 13 '14 at 18:55
  • 1
    We can't help you based on what you've posted. Your real question is "Why isn't my region/category being found?" and you haven't posted anything that could help us tell you. Open a rails console and make sure you can find the records. – user229044 Mar 13 '14 at 18:57
  • @meagar when i run the rails console, i can get both the region and category. For instance when i do: category = Category.where(title: 'football').first : i get Category Load (31.2ms) SELECT categories`.* FROM categories WHERE categories.title = 'football' ORDER BY title LIMIT 1 => # ` – Optimus Pette Mar 13 '14 at 19:28
  • @meager I am not sure whether this helps but when i check the server log, there is no db query being performed. There is no query that should fetch `@region`, `@category` or `@teams`. the application just renders the view. – Optimus Pette Mar 13 '14 at 19:48

1 Answers1

0

Why don't you use find_by (if you're using Rails 4) or find_by_title (if you're using Rails 3):

def filtered_by_region
   @category = Category.find_by_title(params[:category])
   @region = Region.find_by_title(params[:title])

   if defined?(@category) && defined?(@region)
       @teams = Team.where(region_id: region.id, category_id: category.id)
   else
       redirect_to root_path
   end
end

I think the likely issue will either be your query is not finding any records, or you'll be trying to access a collection as a record (regardless of the use of .first)

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I have tried find_by but it still not working, I've realized that the problem is not that the query is not finding any records but that the query is not being fired at all. When i check the server log, the application just renders the view without trying to find the `@category`, `@region` or `@teams` – Optimus Pette Mar 14 '14 at 10:18
  • Very strange. Are you sure it's hitting that controller action? Could be an issue with your routes? – Richard Peck Mar 14 '14 at 10:39
  • Thank you very much, i had made a careless mistake of not closing the method above the `filtered_by_region` method. The application could therefore not find the action hence rendering the view without hitting the action. Your suggestion helped alot. – Optimus Pette Mar 14 '14 at 20:56