當你使用如下命令rails g scaffold Thing
產生導軌支架是有什麼辦法避免讓那個討厭的跳過JSON格式生成腳手架
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
廢話,你打我吧!很好的回答! –