views:

519

answers:

1

Alright. I have a create form where people are able to create businesses which are then added to the database. Everything worked fine. Rails was throwing it's errors if things weren't filled out correctly and if they were, you;d be directed to another page that would show you the business you just created. Now with that, I have to be able to click create and have ajax implement the business and add it to the table below. Things are kind of working. 1 the business is updated but isn't dynamically added to the table. 2. the bigger problem, is that is they don't fill out the required fields, clicking create does nothing. It doesn't throw any of the errors. I'm using jquery to call the ajax through .js.erb files. Here's my .js.erb file.

$("#new_business").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#businesses").append("<%= escape_javascript(render(:partial => @businesses)) %>");
$("#new_business")[0].reset();

and my business controller

    def create
    @business = Business.new(params[:business])

    respond_to do |format|
      if @business.save
        #blindly make them the owner of the business they created
        if current_user
          current_user.business_id = @business.id
          current_user.save
        end
        flash.now[:notice] = 'Business was successfully created.'
        format.html { redirect_to(business_url(@business)) }
        format.js { render :layout => false }
        format.xml  { render :xml => @business, :status => :created, :location => @business }
      else
        flash.now[:notice] = 'Necessary fields have not been filled out'
        format.html { render :action => "new" }
        format.xml  { render :xml => @business.errors, :status => :unprocessable_entity }
      end
    end
  end

The flash notice = Necessary fields have not been filled out doesn't show up unless you refresh the page. None of the default xml errors are showing up either. so if they don't fill out all the correct forms, the button seems unresponsive. How do I use ajax to call those errors ruby is throwing and put them into the flash notice?

+1  A: 

You could refresh the entire form using the form template including the error messages field. Like this you also get missing fields highlighting and so on.

marcgg
Well yea. The whole point is that I'm using Ajax to submit it dynamically to the table below and that there is no page refresh.
Mike
I meant refreshing using ajax. You put the form in a container and update this container
marcgg