rails 4 自定义脚手架配置

13 篇文章 0 订阅
12 篇文章 0 订阅

一.建立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 <h1>这是我自定义的模板</h1>
<h1>这是我自定义的模板</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Pass</th>
      <th>Salt</th>
      <th colspan="3"></th>
    </tr>
  </thead>

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

<br>

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

 

成功!!!

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值