I have classified ads (cads) related with cities and categories:
class Cad < ActiveRecord::Base
has_and_belongs_to_many :categories
has_and_belongs_to_many :cities
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :cads
end
class City < ActiveRecord::Base
has_and_belongs_to_many :cads
end
I assume that user can act such way: first, from cities listing, which
is produced by 'list' method in cities_controller, the user chooses
city ( clicks on 'show' link).
cities_controller#show contains the following:
def show
@city = City.find(params[:id])
@cads = @city.cads@categories_of_cads_in_city = @cads.map {|cad|
cad.categories}.flatten.uniq
end
So, the user sees classified ads and categories of these ads:
The next thing user may want to do - see classified ads in some
category of chosen city (assume in "Health" category) , so he clicks on
link "Health".
So i must find ads which are associated with chosen city and category
"Health". How can i do achieve this ??
Thanks
Many thanks Dan,
i've added method 'category' into 'cities_controller' and its works!
I understand how @category gets its value regarding passed id, but how
can i get the value of @city that was previously set in
cities_controller#show ?
Many thanks Dan,
i've added method 'category' into 'cities_controller' and its works!
I understand how @category gets its value regarding passed id, but how
can i get the value of @city that was previously set in
cities_controller#show ?
I would pass both the ID for the city and the ID for category to your
'category' action.
Example, in the /cities/show view:
<% for category in @categories %>
<%= link_to h(category.name), :action => 'category', :id => @city,
:category => category %>
<% end %>
The with the default routes the URL will be like:
/cities/category/1?category=3
And then in the controller for that action:
@city = City.find(params[:id])
@category = Category.find(params[:category])