rails using has_many and belongs_to -
alright, i'm going try explain best possible:
i have 2 models
employer.rb
class employer < activerecord::base has_many :listings end
listing.rb
class listing < activerecord::base belongs_to :employer end
employers login through employers_controller
, listings created through listings_controller
i'm having trouble getting id employer table being inserted employer_id column in each individual created listing. hope makes sense. if has advice, appreciated. have feeling because i'm doing outside of employer_controller, not sure.
thanks!
1) if not dealing nested resource then
when render new action of listing controller, know employer (@employer) want create listing.
so render hidden field employer_id using hidden_field or hidden_field_tag
hidden_field_tag 'employer_id', @employer.id()
2) if dealing nested resource , route looks /employers/:employer_id/listings/new / (get) && /employers/:employer_id/listings
then in create action
@employer = employer.find(params[:employer_id]) @employer.listing.new(params[:listing]
Comments
Post a Comment