Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize rails generate scaffold

I have a problem, before the command "rails generate scaffold test name: string" generated controllers like this:

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

like image 255
Eduardo Azevedo Avatar asked Nov 01 '22 15:11

Eduardo Azevedo


1 Answers

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.

If this is your case too then quick fix is add following line to aplication.rb file:

config.app_generators.scaffold_controller :scaffold_controller

More info here:

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

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

like image 74
Daniel Dimitrov Avatar answered Nov 15 '22 07:11

Daniel Dimitrov