【问题标题】:Customize rails generate scaffold自定义 rails 生成脚手架
【发布时间】: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 的旧格式 ....

标签: ruby-on-rails scaffolding


【解决方案1】:

这种行为的原因可能是一些宝石。

我遇到了同样的问题,因为最新版本的 Devise 现在随 Responders gem 一起提供。

如果您也是这种情况,那么快速修复方法是将以下行添加到 aplication.rb 文件:

config.app_generators.scaffold_controller :scaffold_controller

更多信息在这里:

https://github.com/rails/rails/issues/17290

https://github.com/plataformatec/responders/issues/94

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2015-02-17
    • 2013-01-11
    • 2013-11-06
    • 2018-12-15
    相关资源
    最近更新 更多