How to get into the GeneratedAttribute in a custom controller generator?

I am creating a custom controller generator that comes from Rails :: Generators :: NamedBase, which creates both the controller and the views, given the specific model name (e.g. Person). I also want to create the _form.html.haml part that builds the form based on the attributes of the model (I use simple_form btw).

What I still have:

<% attributes = file_name.capitalize.constantize.columns.map { |c| [Rails::Generator::GeneratedAttribute.new(c.name, c.type)]} %>
- simple_form_for [:admin,@<%=file_name%>] do |f|
  = render 'shared/error_summary', :object => f.object
  .inputs
  <%- attributes.each do |attribute| -%>
    = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
  <%- end -%>  
  .actions
    = f.button :submit

I get the exception "uninitialized constant Rails :: Generator (NameError)". Not sure what I need, or my approach above is even correct.

Any help would be awesome.

Thanks -wg

+3
source share
1 answer

I suspect the problem is that you are missing s after the generator. The correct method call:

Rails::Generators::GeneratedAttribute.new

, , initialize. :

  def initialize(*args, &block)
    super

    # Call Rails::Generators::GeneratedAttribute.new here

  end

, column_name: column_type, :

class FooGenerator < Rails::Generators::NamedBase
  argument :model_attributes, type: :array, default: [], banner: "model:attributes"

  def initialize(*args, &block)
    super

    @attributes = []

    model_attributes.each do |attribute|
      @attributes << Rails::Generators::GeneratedAttribute.new(*attribute.split(":")) if attribute.include?(":")
    end
  end
end

, , - . , . !

+4

All Articles