How to put javascript in the controller using Rails 3.0.3 and Ruby 1.9.2
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('Invali开发者_C百科d email/password combination.');
I don't want to use the flash message. I really wanted to use javascript alerts... Pls help...
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.
精彩评论