Permalink
Browse files

Make scaffold_controller generator generate code for JSON response in…

…stead of XML

It seems like a lot of people are using JSON in their API more than XML nowadays, so Rails should follow that convention by providing the JSON format block in scaffold_controller by default.

This patch has been requested by DHH
  • Loading branch information...
1 parent 508c679 commit c0efc4009adb870ee23e93d46ac374f772d8cb4f @sikachu sikachu committed with dhh Apr 9, 2011
Showing with 16 additions and 14 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +14 −14 railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
View
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
+* Changed scaffold_controller generator to create format block for JSON instead of XML [Prem Sichanugrist]
+
* Add using Turn with natural language test case names for test_help.rb when running with minitest (Ruby 1.9.2+) [DHH]
* Direct logging of Active Record to STDOUT so it's shown inline with the results in the console [DHH]
@@ -1,35 +1,35 @@
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
# GET <%= route_url %>
- # GET <%= route_url %>.xml
+ # GET <%= route_url %>.json
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
respond_to do |format|
format.html # index.html.erb
- format.xml { render :xml => @<%= plural_table_name %> }
+ format.json { render :json => @<%= plural_table_name %> }
end
end
# GET <%= route_url %>/1
- # GET <%= route_url %>/1.xml
+ # GET <%= route_url %>/1.json
def show
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
format.html # show.html.erb
- format.xml { render :xml => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %> }
end
end
# GET <%= route_url %>/new
- # GET <%= route_url %>/new.xml
+ # GET <%= route_url %>/new.json
def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
respond_to do |format|
format.html # new.html.erb
- format.xml { render :xml => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %> }
end
end
@@ -39,46 +39,46 @@ def edit
end
# POST <%= route_url %>
- # POST <%= route_url %>.xml
+ # POST <%= route_url %>.json
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
respond_to do |format|
if @<%= orm_instance.save %>
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') }
- format.xml { render :xml => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
else
format.html { render :action => "new" }
- format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
+ format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
end
end
end
# PUT <%= route_url %>/1
- # PUT <%= route_url %>/1.xml
+ # PUT <%= route_url %>/1.json
def update
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') }
- format.xml { head :ok }
+ format.json { render :json => {}, :status => :ok }
else
format.html { render :action => "edit" }
- format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
+ format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
end
end
end
# DELETE <%= route_url %>/1
- # DELETE <%= route_url %>/1.xml
+ # DELETE <%= route_url %>/1.json
def destroy
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= orm_instance.destroy %>
respond_to do |format|
format.html { redirect_to(<%= index_helper %>_url) }
- format.xml { head :ok }
+ format.json { render :json => {}, :status => :ok }
end
end
end

14 comments on commit c0efc40

@fxn
Member
fxn commented on c0efc40 Apr 12, 2011

A change like this needs a grep in the project tree for "format.xml" or somesuch, to be able update any existing documentation.

For example, the Getting Started guide needs to be revised. That particular one has been later volunteered via docrails, but let me add a comment about this in the patch as a reminder that grepping is something to take into account in the future :).

@sikachu
Member

Thank you for reminding me, @fxn. I'll update it very soon.

By the way, that's -2 points for me now right? first change method and now this commit >_<

@fxn
Member
fxn commented on c0efc40 Apr 12, 2011

Oh not at all :). You're doing a great job.

Contributions need to take several things into account and one fine-tunes with practice and feedback from others. People needed to be reminded at the beginning that tests were a must. I often forgot the CHANGELOGs when I got my commit right... That's normal, there's a learning process.

Keep them coming :).

@pwnall
pwnall commented on c0efc40 Apr 12, 2011

Awesome! I've been doing this by hand in recent projects. Thank you!!

@nicolasblanco

@pwnall > is scaffolding still used by experimented rails coders? I thought it was just powder to impress newbies ;).

@pwnall
pwnall commented on c0efc40 Apr 12, 2011

@slainer68 > the REST controller logic is pretty decent, I find myself keeping in place quite often. Then again what do I know, I'm a noob :P

@josevalim
Member

You should not be changing it by hand in any case. Rails 3 has a very powerful abstraction that allows you to customize and create your own scaffold.

@fxn
Member
fxn commented on c0efc40 Apr 12, 2011

Scaffolding is meant to be used for prototyping, demos... rarely used in regular code. It never was.

In fact, I rarely need anything but format.html, those .xml and .json are there for demonstration purposes, very much doubt people are writing HTML and API front-ends all the time or whatever. In practice, in my experience it is exceptional that actions need a respond_to block at all.

@pwnall
pwnall commented on c0efc40 Apr 12, 2011

@fxn > Isn't any project's first iteration a prototype?

I think scaffolds + format.json are great because they let me build an XHR-heavy application that gracefully degrades to Web 1.0 pages.

@pwnall
pwnall commented on c0efc40 Apr 12, 2011

@josevalim > If that's how the core team feels, scaffolds should be pulled into a plugin. The generator is in core Rails, and all the books talk about scaffolds. I thought things in core are meant to be used, and I've been teaching students to use scaffolds as well. I'd definitely like a stronger message if they're not supposed to be used.

As a side benefit, pulling scaffolds into a plugin would give me a nice codebase to fork, if I'm supposed to create my own scaffolds. Speaking about that, is the recommended way of doing that documented somewhere?

@josevalim
Member

@pwnall, I said exactly the opposite. You can already customize scaffold today, without forking a codebase or anything. It is a Charlie Sheen situation (bi-winning): we have a great learning tool for people starting with Rails (that books and articles can use) and easy to customize. You can customize it entirely, but in practice you want to customize just parts (like the generated controller and some of the generated views).

Docs: http://guides.rubyonrails.org/generators.html

@dcrec1
dcrec1 commented on c0efc40 Apr 13, 2011

I think scaffold rocks, I use it all the time. This commit will not be useful to me because I love Inherited Resources, but I liked it. Nice one :)

@pwnall
pwnall commented on c0efc40 Apr 13, 2011

@josevalim > Sorry I misunderstood your comment then. I will definitely re-read the docs and look into tweaking the scaffolds.

@pwnall
pwnall commented on c0efc40 Apr 13, 2011

@josevalim > Sorry I misunderstood your comment then. I will definitely re-read the docs and look into tweaking the scaffolds.

Please sign in to comment.