在 rails 中跳过 JSON 格式生成脚手架 [英] Skip JSON format in rails generate scaffold
问题描述
当你使用像 rails g scaffold Thing
这样的命令生成一个 rails scaffold 时,有什么办法可以避免这种烦人的事情
When you generate a rails scaffold using a command like rails g scaffold Thing
is there any way to avoid getting that annoying
respond_to do |format|
format.html # index.html.erb
format.json { render json: @things }
end
控制器中的东西?
我正在尝试教授 Rails 课程,我想先让他们生成一个脚手架,但是所有的 json 格式都比它需要的要复杂得多.如果他们能够生成一个脚手架来创建这样的控制器,我会更高兴:
I'm trying to teach a class on Rails and I'd like to start by having them generate a scaffold, but with all the json formatting it's much more complicated than it needs to be. I'd be much happier if they could generate a scaffold that created a controller like this:
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
推荐答案
在你的 Gemfile
和 respond_to
块中注释掉 gem jbuilder
t 被生成.
Comment out gem jbuilder
in your Gemfile
and respond_to
blocks won't be generated.
这篇关于在 rails 中跳过 JSON 格式生成脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!