I am having a weird problem. I have two models in my app, downtowns and properties. Downtowns should have multiple properties and properties should have one downtown.
On my downtown show page i loop through the properties through each downtown and then offer links to these properties.
Problem is.....when i click on each property, the url goes backwards. IE, a downtown with an ID of 1 and a property with an ID of 8 should go
downtown/1/properties/8
yet it keeps going
downtown/8/properties/1
I think this is so weird and i have no clue what i've been doing wrong here.
On my downtown show page, i loop through each property
%h2= @downtown.name
- if @downtown.properties.present?
%p
- @downtown.properties.collect do |property|
= link_to property.name, downtown_property_path(property)
- else
No downtowns for now.
My routes are the basic nested routing here
resources :downtowns do
resources :properties
end
my properties controller is
class PropertiesController < Downtown::ApplicationController
before_action :find_property, only: [:show, :edit, :update, :destroy]
def new
@property = @downtown.properties.new
end
def create
@property = @downtown.properties.new(property_params)
if @property.save
redirect_to @downtown
else
render :new
end
end
def show
end
private
def property_params
params.require(:property).permit(:name, :downtown, :downtown_id......)
end
def find_property
@property = Property.find(params[:id])
end
end
My downtown controller is
class DowntownsController < ApplicationController
before_action :find_downtown, only: [:show, :edit, :update, :destroy]
def show
@properties = Property.where(downtown: @downtown_id)
end
def new
@downtown = Downtown.new
end
def create
@downtown = Downtown.create(downtown_params)
if @downtown.save
redirect_to @downtown
else
render 'new'
end
end
private
def downtown_params
params.require(:downtown).permit(:name, :city)
end
def find_downtown
@downtown = Downtown.find(params[:id])
end
end
Finally in my properties form i have a polymorphic path
= simple_form_for([@property, @downtown]) do |f|
= f.input :name
= f.input :last_remodel
= f.input :original_construction_year
Nested route path take multiple arguments Rails Guide.
In your case, it should be downtown_property_path(@downtown, property)