2017-10-17 2 views
0

레일 기본 ​​스카 폴드 생성기를 사용자 정의하려고합니다. 여기 lib/templates/erb/scaffold/Rails를 편집하는 방법 Scaffold 모델 생성기

내가 index.html.erb 및 사용자 정의를 추가했습니다,하지만 난이 명령에 의해 생성되는 모델을 변경하려면 : :보기 위해 나는 단순히 아래에 파일을 추가하여 해당 할 수

rails g scaffold model 

I을 같은 코드로 lib 디렉토리/템플릿/레일/모델/model_generator.rb

에 파일을 추가 시도 :

module Rails 
    module Generators 
     class ModelGenerator < NamedBase #metagenerator 
     argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" 
     hook_for :orm, :required => true 

     end 
    end 
    end 

하지만 아무것도하지 않습니다 나는 도움이 필요 내가 n이 점에 관해서는 어떤 파일을 덮어 쓸 필요가 있고 어디에 배치해야합니까?

+0

생성 된 모델 내에서 변경하려는 사항은 무엇입니까? – AnkitG

+0

일부 값을 추가해야 함 레일 g 중 비계 모델 테스트 중에 모델 내부에 몇 가지 사용자 지정 유효성 검사를 넣으려고합니다. 문자열이 코드를 사용하여 모델 안에이 테스트를 넣어야합니다. 가능한 경우 모델 및 컨트롤러를 편집하는 방법을 찾아야합니다. –

+0

수동으로 모델 파일을 만드는 것이 어떻습니까? – sa77

답변

1

여기에 Activerecord 템플릿이 있습니다. 당신은 파일이을 만들어

rails g scaffold property

실행

<% module_namespacing do -%> 
class <%= class_name %> < <%= parent_class_name.classify %> 

    #custom method start 
    before_save :my_custom_method 

    # my method 
    def my_custom_method 

    end 
    #custom method end 

<% attributes.select(&:reference?).each do |attribute| -%> 
    belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %> 
<% end -%> 
<% attributes.select(&:token?).each do |attribute| -%> 
    has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %> 
<% end -%> 
<% if attributes.any?(&:password_digest?) -%> 
    has_secure_password 
<% end -%> 
end 
<% end -%> 

비계 내 사용자 정의 템플릿입니다 lib/templates/active_record/model/model.rb 여기

~/D/p/p/generator_test> tree lib/ lib/ ├── assets ├── tasks └── templates #<======== └── active_record └── model └── model.rb 

로에 넣어 필요

class Property < ApplicationRecord 

    before_save :my_custom_method 

    # my method 
    def my_custom_method 

    end 

end 
+0

굉장한 컨트롤러와 같은 것을하는 방법 lib 폴더에 넣어야하는 파일 –

관련 문제