0

Hello Stackoverflow peeps,

I follow ryan bates railscast about using dynamic pages using the Ancestry gem. I wonder if there is a way to use multiple show pages.

For example I have sections and pages. I want to show a template for the sections and a template for the pages. currently this is my code.

Thanks in advance.

Controller

class PagesController < ApplicationController
    before_filter :find_page, only: [:show, :edit, :update, :destroy]

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @page }
    end
  end

  private
      def find_page
        @page = Page.find_by_slug!(params[:id].split("/").last)
      end   

end

model

class Page < ActiveRecord::Base
  attr_accessible :content, :name, :parent_id, :slug

  validates :slug, uniqueness: true, presence: true,
                 exclusion: {in: %w[signup login]}

  before_validation :generate_slug

  has_ancestry

  def to_param
    slug
  end

  def generate_slug
    self.slug ||= name.parameterize
  end

  def find_page
    @page = Page.find_by_slug(params[:id].split('/').last)
  end
end

I would like to have for example two different template for the show.html.erb page. Is that possible?

4

1 回答 1

0

我不确定我是否理解。可能是这样的:

def show
  respond_to do |format|
    format.html do
      if @page.parent_id
        render <path to page view>
      else
        render <path to section view>
      end
    end

    format.json { render json: @page }
  end
end
于 2013-05-10T12:41:19.603 回答