AJAX is good for a lot of things, but javascript is not always
available.
I think the new RJS templates are very good:
http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates
And they could be used to create a simple, automatic no javascript
fallback. An example:
You have a controller called PostsController. There is an action “new”.
The view displays a form. The form will be submitted to the
“create”-action using AJAX, and if the user doesn’t have javascript
enabled, it will be submitted to the create action anyway. It looks like
this:
class PostsController < ApplicationController
def create
@post = Post.new(params[:post])
@post.save
end
end
This creates a new post. There are two views: one create.rhtml and one
create.rjs. Create.rhtml is just usual ruby-html code and create.rjs
contains javascript code that will be executed. Rails determines which
template to use with request.xhr?. If this is an xmlhttprequest: render
create.rjs, else render create.rhtml.
Would it be possible to do this with an after filter? The filter checks
if there are two templates: #{action}.rjs and #{action}.rhtml and
renders the correct one.
I will need to change the view helpers too to add automatic ajax and non
ajax submit.
Is this a good idea? How could the view helpers be implemented?