【发布时间】:2014-10-26 02:41:58
【问题描述】:
我有一个问题,在命令“rails generate scaffold test name: string”之前生成的控制器是这样的:
class Teste < ApplicationController
before_action :set_teste, only: [:show, :edit, :update, :destroy
# GET /testes
# GET /testes.json
def index
@testes = Teste.all
end
# GET /testes/1
# GET /testes/1.json
def show
end
# GET /testes/new
def new
@teste = Teste.new
end
# GET /testes/1/edit
def edit
end
# POST /testes
# POST /testes.json
def create
@teste = Teste.new(teste_params)
respond_to do |format|
if @teste.save
format.html { redirect_to testes_path, notice: 'Teste cadastrado.' }
format.json { render :show, status: :created, location: @teste }
else
format.html { render :new }
format.json { render json: @teste.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /testes/1
# PATCH/PUT /testes/1.json
def update
respond_to do |format|
if @teste.update(teste_params)
format.html { redirect_to testes_path, notice: 'Teste atualizado.' }
format.json { render :show, status: :ok, location: @teste }
else
format.html { render :edit }
format.json { render json: @teste.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testes/1
# DELETE /testes/1.json
def destroy
@teste.destroy
respond_to do |format|
format.html { redirect_to testes_url, notice: 'Teste excluído.' }
format.json { head :no_content }
end
end
不知道为什么,但是现在正在生成另一种格式
class TestesController < ApplicationController
before_action :set_teste, only: [:show, :edit, :update, :destroy]
def index
@testes = Teste.all
respond_with(@testes)
end
def show
respond_with(@teste)
end
def new
@teste = Teste.new
respond_with(@teste)
end
def edit
end
def create
@teste = Teste.new(teste_params)
@teste.save
respond_with(@teste)
end
def update
@teste.update(teste_params)
respond_with(@teste)
end
会是什么?为什么这发生了变化?
我会以以前的格式返回,因为我的整个系统都是第一种格式
【问题讨论】:
-
你已经升级了 Rails。现代 Rails 使用
respond_with而不是respond_to do...。老实说,您可能不应该过多依赖脚手架生成,因为您很快就会长大。 -
脚手架是一种很好的入门方式,但正如 meagar 所指出的那样,脚手架很容易长大。
-
我也有这个问题。我怎样才能恢复到以前的格式?
-
我没有对我的应用程序和他们的 gem 做任何事情,它怎么会出现一个新的脚手架格式?我尝试创建一个新应用程序,尝试生成一个脚手架,它使用具有 response_to 的旧格式 ....