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 %>');
});


2 comments:
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
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
Post a Comment