如何在Rails 3中链接到对象的父对象?

rug*_*ert 1 ruby-on-rails ruby-on-rails-3

如果我有以下型号:

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

    class Page < ActiveRecord::Base
      belongs_to :section
    end
Run Code Online (Sandbox Code Playgroud)

我有一个拥有页面的版块,如何添加一个link_to链接到该页面的父级?或者,如何找到页面的所有者?

Ste*_*oss 5

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

或者,在视图中:

<%= link_to "section", @page.section %>
Run Code Online (Sandbox Code Playgroud)