加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在rails中跳过JSON格式生成脚手架

发布时间:2020-12-16 20:03:15 所属栏目:百科 来源:网络整理
导读:当您使用像rails g脚本这样的命令生成轨道脚手架时,有什么办法可以避免让人讨厌 respond_to do |format| format.html # index.html.erb format.json { render json: @things }end 你的控制器中的东西? 我试图在Rails上教一个课程,我想从他们生成一个脚手架
当您使用像rails g脚本这样的命令生成轨道脚手架时,有什么办法可以避免让人讨厌
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @things }
end

你的控制器中的东西?

我试图在Rails上教一个课程,我想从他们生成一个脚手架开始,但是所有的json格式化都比它需要的要复杂得多.如果他们可以生成一个创建一个这样的控制器的脚手架,我会更快乐:

class ThingsController < ApplicationController

  def index
    @things = Thing.all
  end

  def show
    @thing = Thing.find(params[:id])
  end

  def new
    @thing = Thing.new
  end

  def edit
    @thing = Thing.find(params[:id])
  end

  def create
    @thing = Thing.new(params[:thing])
      if @thing.save
        redirect_to @thing,notice: 'Thing was successfully created.'
      else
        render: "new" 
      end
    end
  end

  def update
    @thing = Thing.find(params[:id])
      if @thing.update_attributes(params[:thing])
        redirect_to @thing,notice: 'Thing was successfully updated.'
      else
        render: "edit" 
      end
    end
  end

  def destroy
    @thing = Thing.find(params[:id])
    @thing.destroy
    redirect_to things_url
  end
end

解决方法

只需克隆文件

https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb

到你的

lib/rails/generators/rails/scaffold_controller/templates/controller.rb

您的应用程序中的路径,并自定义您想要的.此外,您可以编写自己的脚手架发电机(http://guides.rubyonrails.org/generators.html).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读