· 1 min read

Screencast: Eigene Rails 3 Generatoren erstellen

Download:

Download(14 MB, 10:07) Alternativer Downloadfür iPod & Apple TV(13.8 MB, 10:07)

Resourcen:

Quellcode:

[bash] rails g generator —help rails g generator layout rails g layout —help rails g layout admin rails g layout foo —skip-stylesheet [/bash]

[ruby] # lib/generators/layout/layout_generator.rb class LayoutGenerator < Rails::Generators::Base source_root File.expand_path(‘../templates’, __FILE__) argument :layout_name, :type => :string, :default => “application” class_option :stylesheet, :type => :boolean, :default => true, :desc => “Include stylesheet file.” def generate_layout copy_file “stylesheet.css”, “public/stylesheets/#{file_name}.css” if options.stylesheet? template “layout.html.erb”, “app/views/layouts/#{file_name}.html.erb” end private def file_name layout_name.underscore end end [/ruby]

[html] # lib/generators/layout/templates/layout.html.erb

Untitled<%- if options.stylesheet? -%> <%%= stylesheet_link_tag ”<%= file_name %>” %> <%- end -%> <%%= javascript_include_tag :defaults %> <%%= csrf_meta_tag %> <%%= yield(:head) %>

<%% flash.each do |name, msg| %> <%%= content_tag :div, msg, :id => “flash_#{name}” %> <%% end %> <%%= yield %>
[/html]

Back to Blog