wiredin
wiredin

Reputation: 269

Rails model associations and controllers/routes

I have setup an association where I have Shows that are associated to Cities.

The relationship is many shows to one city.

I would like to create new shows by city

Like such: mysite.com/cities/1/shows/new

I have setup the models already so that:

Shows
Belongs_to :city

City
Has_many :shows

How do I setup my City controller with a new shows action? How to I setup my routes.rb for this?

I've figured it out

In routes.rb

resources :cities do
  resources :shows
end

Upvotes: 1

Views: 99

Answers (1)

ex0ns
ex0ns

Reputation: 1116

You could probably use a rule such as :

match /cities/:id/shows/new => "shows#new"

And after, in your new action in the controller shows, you could preload the city_id to be params[:id], something as

def new
    @show = Show.new(:city_id => params[:id])
end

Upvotes: 1

Related Questions