views:

322

answers:

4

I am trying to display an error message of flash in Ajax using Rails but it doesn't display it, and I have tried several ways to do it but I just can't get it right.

Here is my form to display my errors:

#flash-error.flash-error{:style => "display: none"}
#flash-notice.flash-info{:style => "display: none"}
   %h3 New Document 
    - form_for(:repo_document, :url => {:all_categories => @repo_categories, :action =>  "create", :format => "js", :query => params[:query]}, :html => { :id => "repo_document_form", :multipart => true, :target => 'upload_frame'}) do |form|

Here is the controller:

  if !params[:stands]
     respond_to do |format|
        format.js do
          responds_to_parent do
            render :update do |page|
              page.show "flash-error"
              page.replace_html "flash-error","Please select stands you want to grant permission to view this stand." 
            end
          end
        end
        return 
     end
  end

What am I doing wrong?

A: 

A possible solution would be to send an ajax call to an action in a controller and set your flash messages in that action

dimus
I am using form_for for sending the ajax and not using the form_remote_tag.. because i am uploading a file..and it doesn't support the form_remote_tag.
Donald
A: 

Flash is only going to display on the next time the page is loaded. It doesn't update through AJAX. One of the caveats of AJAX is that it doesn't deal with file uploads...

See this for more details; http://guides.rubyonrails.org/form%5Fhelpers.html#uploading-files

Richard Seviora
+1  A: 

js.rjs to the rescue

def action
  @true = params[:stands]
  respond_to |format|
    format.js
  end
end

in action.js.rjs:

if @true
  # do true stuff
else
  page.replace_html 'error_block_dom_id', :text => 'This is my error'
end

and the original view file that you make the ajax call from:

#nothing here due to no errors - errors will appear if they happen
<div id="error_block_dom_id"></div>

No overarching reason to use the flash - just update a DOM object with the error content.

inkdeep
A: 

I think the problem might be elsewhere: Element's method show (call of which is generated when You call page.show "flash-error") will not remove display: none css style when it is given inside the external Css file. This is a quotation from older version of Prototype's API Documentation.

So try to add display: none as inline style of Your div's. I hope it will help!

Dejw