rugbert
rugbert

Reputation: 12653

How can I link to and object's parent in Rails 3?

If I have the following models:

    class Section < ActiveRecord::Base
      has_many :pages, :dependent => :destroy
    end

    class Page < ActiveRecord::Base
      belongs_to :section
    end

And I had a section owning a page, how could I add a link_to that would link to that pages parent? Or, how could I find a page's owner?

Upvotes: 1

Views: 243

Answers (1)

Steve Ross
Steve Ross

Reputation: 4144

@page      = Page.find(params[:id])  # or whatever the criteria
@page_link = link_to "section", @page.section

Or, in the view:

<%= link_to "section", @page.section %>

Upvotes: 5

Related Questions