当您使用rails g scaffold Thing
之类的命令生成rails scaffold时,有没有什么方法可以避免这种恼人的做法
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
发布于 2014-06-08 09:35:13
注释掉Gemfile
中的gem jbuilder
,并且不会生成respond_to
块。
发布于 2012-12-25 05:00:54
只需克隆文件即可
致您的
lib/rails/generators/rails/scaffold_controller/templates/controller.rb
路径,并自定义所需的内容。此外,您还可以编写自己的脚手架生成器( http://guides.rubyonrails.org/generators.html )。
发布于 2014-02-28 05:15:51
我觉得你会错过一个机会。首先,你将教授非标准的Rails,所以当你的学生在他们自己的安装中看到普通版本时,他们可能会感到困惑。
更重要的是,控制器以这种方式格式化是有原因的。Rails强调REST,它鼓励通过多种数据格式访问资源。许多现代应用程序正在淡化较慢的服务器渲染的html/erb响应,转而使用json。我意识到这是在你的作业之后的一年多一点,你在课堂上的时间有限,只是为可能发生的任何人添加一些想法。我认为你可以在respond_to上挥舞你的手,告诉他们它正在为你未来的一些可能性做准备。
https://stackoverflow.com/questions/14027644
复制相似问题