当您使用类似命令生成 rails 脚手架时rails g scaffold Thing
,有什么方法可以避免烦人
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