Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 在rails中跳过JSON格式生成脚手架大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您使用像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 @R_675_6048@sfully 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 @R_675_6048@sfully 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).

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 在rails中跳过JSON格式生成脚手架全部内容,希望文章能够帮你解决ruby-on-rails – 在rails中跳过JSON格式生成脚手架所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。