so simplify have, there 3 main models in application; city, restaurant , recipe. city has many restaurants , restaurant has many recipes. every city has page lists restaurants in city , every restaurant page has recipes listed in page. there "add new restaurant" button in city page, when user clicks on user taken new restaurant page following link:
<%= link_to 'add new restaurant', new_restaurant_path %>
but page generic page user can add restaurant city, how modify design that, new restaurant form add new restaurant city.
edit: answers. restaurant create method right now.. because new_restaurant_path form , has other params city. understand can figure out city of restaurant doing @city = city.find(params[:city]) how add rest of params in line @restaurant = restaurant.new(params[:restaurant])
def create @restaurant = restaurant.new(params[:restaurant]) end
with routes
you can create route this:
/cities/:city_id/restaurants/new
with:
resources :city resources :restaurants end
and helper:
<%= link_to 'add new restaurant', new_city_restaurant_path(city) %>
with params in helper
or can pass parameter in current route helper , handle in controller:
<%= link_to 'add new restaurant', new_restaurant_path(city_id: city.id) %>
this make use url:
/restaurants/new?city_id=123
for both approaches, controller
and in controller:
def create city = city.find(params[:city_id]) @restaurant = city.restaurants.build params[:restaurant] if @restaurant.save ... end
Comments
Post a Comment