简体   繁体   English

自定义rails生成脚手架

[英]Customize rails generate scaffold

I have a problem, before the command "rails generate scaffold test name: string" generated controllers like this: 我有一个问题,在命令“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

Do not know why, but this is now generating another format 不知道为什么,但现在这又产生了另一种格式

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

What can it be? 它能是什么? Why has this changed? 为什么这会改变?

I would return in the previous format because my whole system is in the first format 我会以之前的格式返回,因为我的整个系统是第一种格式

The reason of this behavior is probably some gem. 这种行为的原因可能是一些宝石。

I had same problem because latest version of Devise which is now shipped with Responders gem. 我有同样的问题,因为最新版本的Devise现在随Responders gem一起发售。

If this is your case too then quick fix is add following line to aplication.rb file: 如果这也是你的情况,那么快速修复是在aplication.rb文件中添加以下行:

config.app_generators.scaffold_controller :scaffold_controller

More info here: 更多信息:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM