gpt4 book ai didi

ruby-on-rails - 如何从 scaffold_controller 模板中删除 respond_to block

转载 作者:数据小太阳 更新时间:2023-10-29 07:53:10 27 4
gpt4 key购买 nike

如何自定义脚手架生成器# was following this link

类 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

如何删除所有的respond_to代码?

最佳答案

使用 respond_with 让您的 Controller 更干净。 This apidocthis screencast将回答您所有的相关问题。

您的 Controller 方法将像这样干净:

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

要将其应用于默认脚手架 Controller 模板,只需从github 复制模板内容即可。并将其放入 RAILS_ROOT/lib/templates/rails/scaffold_controller/controller.rb。然后在那里应用 respond_with 方法。

关于ruby-on-rails - 如何从 scaffold_controller 模板中删除 respond_to block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com