Class: Formtastic::FormGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/formtastic/form/form_generator.rb

Overview

Generates a Formtastic form partial based on an existing model. It will not overwrite existing files without confirmation.

Examples:

$ rails generate formtastic:form Post

Copy the partial code to the pasteboard rather than generating a partial

$ rails generate formtastic:form Post --copy

Return HAML output instead of ERB

$ rails generate formtastic:form Post --haml

Generate a form for specific model attributes

$ rails generate formtastic:form Post title:string body:text

Generate a form for a specific controller

$ rails generate formtastic:form Post --controller admin/posts

Instance Method Summary collapse

Instance Method Details

#create_or_showObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/formtastic/form/form_generator.rb', line 36

def create_or_show
  @attributes = reflected_attributes if @attributes.empty?
  
  if options[:copy]
    template = File.read("#{self.class.source_root}/_form.html.#{template_type}")
    erb = ERB.new(template, nil, '-')
    generated_code = erb.result(binding).strip rescue nil
    puts "The following code has been to the clipboard, just paste it in your views:" if save_to_clipboard(generated_code)
    puts generated_code || "Error: Nothing generated. Does the model exist?"
  else
    empty_directory "app/views/#{controller_path}"
    template "_form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
  end
end