rails 4 自定义脚手架配置

阅读更多

一.建立rails 4 项目

rails -v # => 4.1.7
rails new customgenerator --skip-bundle
# edit the gem source in Gemfile
cd customgenerator
bundle install
rails s
# visit http://127.0.0.1:3000/

 

二.添加脚手架配置

# in application.rb

config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  # g.test_framework  :test_unit, fixture: false
  g.test_framework  nil
  g.stylesheets     false
  g.javascripts     false
  g.jbuilder        false
  g.helper          false
end

 

根据需求自己配置,我的这样就干净多了.

然后下载rails 4.1.7,解压.

在lib文件夹下建立templates/erb/scaffold文件夹

# https://github.com/rails/rails/tree/master/railties/lib/rails/generators/erb/scaffold/templates
# copy the five *.html.erb file to lib/templates/erb/scaffold/
# note: 不要后面的templates

 

三.使用

rails g scaffold User name:string pass:string salt:string

 

# the result
      invoke  active_record
      create    db/migrate/20141104024358_create_users.rb
      create    app/models/user.rb
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke  assets
      invoke    coffee
      invoke    scss

 

四.按照你的需要修改者五个文件

rails g scaffold Customuser name:string pass:string salt:string

# the result
      invoke  active_record
      create    db/migrate/20141104024557_create_customusers.rb
      create    app/models/customuser.rb
      invoke  resource_route
       route    resources :customusers
      invoke  scaffold_controller
      create    app/controllers/customusers_controller.rb
      invoke    erb
      create      app/views/customusers
      create      app/views/customusers/index.html.erb
      create      app/views/customusers/edit.html.erb
      create      app/views/customusers/show.html.erb
      create      app/views/customusers/new.html.erb
      create      app/views/customusers/_form.html.erb
      invoke  assets
      invoke    coffee
      invoke    scss


# 我修改了 index.html.erb 

这是我自定义的模板

这是我自定义的模板

<% @customusers.each do |customuser| %> <% end %>
Name Pass Salt
<%= customuser.name %> <%= customuser.pass %> <%= customuser.salt %> <%= link_to 'Show', customuser %> <%= link_to 'Edit', edit_customuser_path(customuser) %> <%= link_to 'Destroy', customuser, method: :delete, data: { confirm: 'Are you sure?' } %>

<%= link_to 'New Customuser', new_customuser_path %>

 

成功!!!

 

项目源代码: https://github.com/FlowerWrong/customgenerator

你可能感兴趣的:(rails,custom,generator)