Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove respond_to block from scaffold_controller template

how to customize scaffold generator #was following this link

class IdeasController < ApplicationController before_action :set_idea, only: [:show, :edit, :update, :destroy]

  # GET /ideas
  # GET /ideas.json
  def index
    @ideas = Idea.all
  end

  # GET /ideas/1
  # GET /ideas/1.json
  def show
  end

  # GET /ideas/new
  def new
    @idea = Idea.new
  end

  # GET /ideas/1/edit
  def edit
  end

  # POST /ideas
  # POST /ideas.json
  def create
    @idea = Idea.new(idea_params)

    respond_to do |format|
      if @idea.save
        format.html { redirect_to @idea, notice: 'Idea was successfully created.' }
        format.json { render action: 'show', status: :created, location: @idea }
      else
        format.html { render action: 'new' }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ideas/1
  # PATCH/PUT /ideas/1.json
  def update
    respond_to do |format|
      if @idea.update(idea_params)
        format.html { redirect_to @idea, notice: 'Idea was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ideas/1
  # DELETE /ideas/1.json
  def destroy
    @idea.destroy
    respond_to do |format|
      format.html { redirect_to ideas_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_idea
      @idea = Idea.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def idea_params
      params.require(:idea).permit(:name, :description, :picture)
    end
end

How to remove all the respond_to code ?

like image 596
Sakshi Jain Avatar asked Dec 31 '25 18:12

Sakshi Jain


1 Answers

Use respond_with to make your controllers cleaner. This apidoc and this screencast will answer all your related questions.

Your controller methods will be as clean as this:

def update
  @idea.update(idea_params)
  respond_with @idea, notice: 'Idea was successfully updated.'
end

To apply that to default scaffold controller template, just copy the template content from github and put it into RAILS_ROOT/lib/templates/rails/scaffold_controller/controller.rb. Then apply the respond_with approach there.

like image 185
Konstantin Rudy Avatar answered Jan 02 '26 11:01

Konstantin Rudy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!