Forms within other object views

I’m sure I’m overlooking something really obvious, but I’m trying to
figure out how to do a form for an object associated with the current
view object.

Specifically, I have an itinerary that has events. When I’m looking at
an itinerary I should be able to add new events. My relationship
between the two is:

class Itinerary < ActiveRecord::Base
has_many :event
end

class Event < ActiveRecord::Base
has_one :itinerary
end

In the itineraries_controller, I have a show method defined like this:

def show
@itinerary = Itinerary.find(params[:id])

@new_event = Event.new

end

However, it is clearly puking on the Event.new, and I’m looking for a
good place to learn about how to work through all of these
associations. If this isn’t the place, I’d love a link to where this
is taught.

Thanks in advance.

Melih O.

So I’ve updated my show method to look like:

def show
@itinerary = Itinerary.find(params[:id])
@new_event = @itinerary.events.build
end

but still no luck.

Thanks again.

Melih O.

Got it.

I had a has_one relationship where I needed a belongs_to relationship.

Thanks interwebs :slight_smile:

Melih

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs