0
votes

Can somebody please tell me how to put javascript in the controller. I tried someting like this in the sessions controller:

def sign_in
  if user.nil?
      @title = "Sign in"
      render :js => "alert('Invalid email/password combination.');"
  else
      sign_in user
      redirect_back_or user      
  end
end

when I tried that, the page displays in text format: alert('Invalid email/password combination.');

I don't want to use the flash message. I really wanted to use javascript alerts... Pls help...

1
Apparently its not working, check this gist gist.github.com/416046 . use javascript_tagZimbabao

1 Answers

3
votes

For the UJS way do this:

in your controller.rb

def sign_in
  if user.nil?    
      @title = "Sign in"    
      respond_to do |format|            
          format.js    
      end
    end    
  else    
      sign_in user    
      redirect_back_or user          
  end    
end

*in sign_in.js.erb*

alert('Invalid email/password combination.');

This will give you a nice separation of church and state. All ruby code in the controller.rb, all javascript in the view.js.erb.