Reputation: 683
My models are;
class region
has_many :cities
end
class event
belongs_to :city
end
class city
has_many :events
belongs_to :region
end
Eventcontroller
def index
@region = Region.find(params[:region_id])
@cities = @region.cities
@city = City.find(params[:city_id])
@events = @city.events
@events_by_date = @events.group_by(&:start_on)
end
Page: region/x/cities/x/events
Shows all the events from the city.
Question: how can i show all events from all the cities on my /region/events page? I created a resources
resources :regions do
resources :events do
collection do
get 'all_events'
end
end
But how I define the "all_event" action in the eventcontroller?
I cannot do this in my eventcontroller @events = @cities.events because event belongs_to :city. Is there a solution for this?
I tried this:
controller:
@region = Region.find(1)
@events = Event.includes(:city)
@events_by_date = @events.group_by(&:published_on)
View:
%ul.property_list
- @events_by_date[date].each do |event|
%li.restaurant
%span.icon
%h5
= link_to event.title, polymorphic_path([@region, event.city.name, event])
error : undefined method `region_Assisi_event_path'
Upvotes: 0
Views: 65
Reputation: 4097
If I am understanding this correctly all you would need to do:
In your controller
def all_events
@events = Event.includes(:city)
end
Then in your view, use a block to access all the instances
@events.each do |event|
event.name
event.city.name #not sure if you have a name attribute
Upvotes: 1