仅在我写render时布局渲染:layout =>每个资源上的“应用程序”

时间:2012-02-06 23:16:49

标签: ruby-on-rails-3

代码:https://github.com/werner/inmailing, 我试图在控制器中使用继承,但由于某种原因布局以这种方式工作,我想我做错了什么,但我看不到它。

class StandardController < ApplicationController
  attr_accessor :model, :url
  handles_sortable_columns

  def index
    @records = @model.all
    @q = model.search params[:q]
    @records = @q.result(:distinct => true).order(sortable_column_order).paginate(:page => params[:page], :per_page=>10)

    #I'm not sure why I have to do this on every method
    #but if I don't rails doesn't recognize layout
    render :layout => 'application'
  end

  def new
    @record = @model.new
    render :layout => 'application'
  end

  def edit
    @record = @model.find(params[:id])
    render :layout => 'application'
  end

  def show
    @record = @model.find(params[:id])
    render :layout => 'application'
  end

  def create
    @record = @model.new(params[@model.to_s.downcase])

    respond_to do |format|
      if @record.save
        format.html { redirect_to @url, notice: 'Successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end

  def update
    @record = @model.find(params[:id])

    respond_to do |format|
      if @record.update_attributes(params[@model.to_s.downcase])
        format.html { redirect_to @url, notice: 'Successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

  def destroy
    @record = @model.find(params[:id])
    @record.destroy

    respond_to do |format|
      format.html { redirect_to @url }
    end
  end
end


class DepartmentsController < StandardController 

  before_filter do
    @url = departments_url
    @edit_record_path = lambda {|record| edit_department_path(record) }
    @new_record_path = new_department_path
    @title = "Departments" 
  end

  def initialize
    @model = Department
  end
end

布局:

!!! XML
!!!
%html
  %head
    %title Inmailing
    = stylesheet_link_tag 'application'
    = stylesheet_link_tag 'blueprint/screen.css', :media => 'screen, projection'
    = stylesheet_link_tag 'blueprint/print.css', :media => 'print'
    /[if IE]
      = stylesheet_link_tag 'blueprint/ie.css', :media => 'screen, projection'    
    = csrf_meta_tags
  %body
    = yield
    = javascript_include_tag 'application'

1 个答案:

答案 0 :(得分:0)

您可以在application_controller中指定布局。

只需添加此行即可。应用程序必须在layout / application.html.erb

layout 'application'