I've been searching with not much luck for this... I have the code
url开发者_开发技巧 = params[:url];
if url == ""
@myString = "No URL specified, cannot continue"
else
=begin
DO SOME STUFFS....
=end
end
But the DO SOME STUFFS is still getting called. I have my index page which I perform actions on the _GET variables. I'd like to load the index page without the parameters in the URL. How do I check if they are set - if they're not, then display a message. If they are, process the requests...
Thanks!
Try this:
url = params[:url];
if params[:url].nil?
@myString = "No URL specified, cannot continue"
else
DO SOME STUFFS....
end
When params[:url]
is empty it's equal to nil
, not ""
.
use if url.blank?
blank? checks for nil and empty string and if you are not passing the url then accessing params[:url] would return nil. That is why your condition is failing.
rails 5:
@myString = "No URL specified, cannot continue" unless params[:url]
Short and simple
url = params[:url]
if url
# do stuff
else
@my_string = "No URL specified, cannot continue"
end
params[:url]
will be nil
if not provided. nil
evaluates false in a conditional.
精彩评论