Search code examples
javascriptruby-on-railsruby-on-rails-3.1rails-3.1

How do you render your views with Ajax in RoR 3.1?


I have a controller with a create action and I want to render the new action when there is and error and to redirect to another controller when the save is succesful.

For the moment, I have this kind of method :

def create 
  @user = User.new(params[:user])
  unless @user.save
    render :new
  end
end

And I have a app/views/MyController/myAction.js.erb which display a message and do Ajax call to my other controller.

It works but I'm not sure than that's the best way. Have you some suggestion? Do you know an Open Source project which use a lot of Ajax.


Solution

  • Take a look at this article I wrote last week.

    http://ror-e.com/posts/20-asset-pipeline-best-practices

    you might need to learn how to namespace in JS but once you do your JS will be much better.