当您使用像rails g脚本这样的命令生成轨道脚手架时,有什么办法可以避免让人讨厌
respond_to do |format| format.html # index.html.erb format.json { render json: @things } end
你的控制器中的东西?
我试图在Rails上教一个课程,我想从他们生成一个脚手架开始,但是所有的json格式化都比它需要的要复杂得多.如果他们可以生成一个创建一个这样的控制器的脚手架,我会更快乐:
class ThingsController < ApplicationController def index @things = Thing.all end def show @thing = Thing.find(params[:id]) end def new @thing = Thing.new end def edit @thing = Thing.find(params[:id]) end def create @thing = Thing.new(params[:thing]) if @thing.save redirect_to @thing,notice: 'Thing was successfully created.' else render: "new" end end end def update @thing = Thing.find(params[:id]) if @thing.update_attributes(params[:thing]) redirect_to @thing,notice: 'Thing was successfully updated.' else render: "edit" end end end def destroy @thing = Thing.find(params[:id]) @thing.destroy redirect_to things_url end end
解决方法
只需克隆文件
到你的
lib/rails/generators/rails/scaffold_controller/templates/controller.rb
您的应用程序中的路径,并自定义您想要的.此外,您可以编写自己的脚手架发电机(http://guides.rubyonrails.org/generators.html).