rails ajax form saving, redirecting, updating issues

I'm trying to create an ajax enabled form in rails. When I click the submit button, the form is saving properly (I can see the update taking place in the database), however, the page just sits there (looks as though nothing is happening, no redirect, etc) and in my web inspector, I see the following error:

POST http://localhost:3000/user/mike 500 (Internal Server Error)

However, when I click the above link (http://localhost...) the page is displayed including the :notice.

I would like the page to redirect as it normally would and show the :notice if available. I would also like to be able to save the form over ajax, without redirect on a certain time interval (every minute or so). Is this possible?

user_controller.rb

def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
         format.html { redirect_to(@user, :notice => 'User saved.') }
          format.js

        else

          format.html { render :action => "new" }
          format.js
        end
    end
  end

  def update
    @user = User.find_by_username(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])

        format.html { redirect_to(@user, :notice => 'User updated.') }
        format.js

      else

        format.html { render :action => "edit" }
        format.js

      end
    end
  end

And, here is my form tag:

<%= form_for @user, :remote => true, :html => { :multipart => true, :class => 'edit_profile' }  do |f| %>

Any suggestions would be awfully helpful...Thanks everyone.

Answers


If you want the create action to be ajax, you'll want to add a create.js template (create.js.erb or create.js.haml), that will provide some script to render a message on the page.

(assuming jquery and ERB) -- create.js.erb:

<% if @user.errors.any? %>
    //update a div here with the errors collection
<% else %>
    $("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
<% end %>

You would need some additional logic in there if an error occurred to show a list of errors to the user. The easiest would be if the form was already in a partial, since you could just re-render the partial showing the errors collection. Alternatively, you could just write out the errors collection to a div.

As for updating periodically in the background, a simple javascript timer that posts the form would work fine. In the controller, you could either render nothing (using render nothing: true), or provide an update.js script to change a message such as "Last Saved at xxxx".


Need Your Help

Time - hour minute manipulation

java

Is there an API to quickly manipulate (e.g. add, subtract) on time (hour, minute).

How to catch exceptions in workers in Multiprocessing

python python-2.7 multiprocessing

I'm working with the multiprocessing module in Python (2.7.3) and want to debug some stuff going on in my workers. However, I seem to not be able to catch any exceptions in the worker threads.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.