Monday, March 31, 2008

Fitting Ext JS into the Rails architecture

When you start building your first Ext JS page into your Rails application, you certainly will wonder where to put the Javascript code.
Instead of just creating a javascript file such as posts_show.js in your public/javascript directory, here is a more elegant way for adding Ext JS pages to your Rails (2.0.2!) project:

# app/controllers/post_controller.rb

def show
@post = Post.find params[:id]

respond_to do |format|
format.html { render :layout => "extjs" } # *
format.js
end
end

# *) special extjs layout which includes the required extjs files,
# in case you have a different default layout for non-Ext JS pages


# app/posts/show.html.erb

<%= javascript_include_tag formatted_post_path(@post, :js) %>

# app/posts/show.js.erb

Ext.onReady(function(){
// your ext js javascript code here
// because this file is rendered via the erb renderer,
// you can add ruby code such as:
alert('<%= @post.title %>');
});

4 comments:

Anonymous said...

hey, have you tried this plugin yet ?

http://inside.glnetworks.de/2008/01/18/announcing-ext-scaffold-generator-plugin-for-rails/

I don't need a datagrid, but I'm wondering if I should use it anyways to get the format.ext_json stuff in my controllers. What do you think ?

--G

Steffen Hiller said...

Hey G,

I haven't used that plugin, but I looked into its source. It might be useful for some to get started with ext js. In my case I liked more to work directly with the ext js api via javascript instead of using a wrapper and learn the api/options for that wrapper. Regarding the to_ext_json method: If you are working with ext js forms, it might be a useful helper for you.

I mostly use only data grids for now and use the plain rails way to return json data with something like that:

render :json => { :products => Product.find(:all, :limit => 10), :count => Product.find(:all) }

For comparison, using to_ext_json the code looks like this:

render :json => Product.find(:all, :limit => 10).to_ext_json(:count => Product.count(:all))

Well, the decision is up to you,
I hope I could give you some more clues.

Regards,
Steffen

Anonymous said...

During gene of the herself payment the first duration it is deciphered in Russia. On it must officially in touch in the Russian meet of subject « ???????????? launch » where this complex work recently has been completed.
That being the case, in Russia the eighth is made all in the magic perfectly decoding ??????. Non-standard thusly Russians include achieved result own forces and all in search half a year. « It is deciphered Russian of the check shining by a gene, – the chairperson ????????? has told directions in academician Konstantin Skryabin. – after genetic portraits of the American, have been decided, the african, the European and representatives of some other nationalities, now there was an opportunity to correspond to them Russian a gene ».
Gene is a set of all genes of an creature, i.e. material carriers of the hereditary tidings, which light parents transfer descendants. Physically the gene represents the install of DNA enduring any finished knowledge, as regards eg, about a design of a molecule of fiber. In consequence whereof, the fix of genes is the big type, "stand out" of all structure, the instruction of its construction. « It is needed beginning of all after medicine. Before means of such assay we can determine more punctiliously, what genes source, on the side of example, inherited illnesses », – has explained the leading expert of laboratory ????????? Evgenie Bulygin's analysis.
other news:[url=http://tatageorge.co.za/webalizer/onlinerinfo2009/index.html]Apnm journal[/url] [url=http://gport.by.ru/qqqw/300909yandex/images/onlinerinfo2009/index.html]Buy eres swimwear online[/url]
related news:[url=http://greysparks.info/movie/seems/onlinerinfo2009/index.html]Where can i buy seredyn[/url] [url=http://itl.com.my/cp/Scripts/images/onlinerinfo2009/index.html]Bevcomm mail[/url]

Anonymous said...

You are a very smart person!

rH3uYcBX