url encode equivalent in ruby on rails - ruby-on-rails

Is there an equivalent to PHP's urlencode in Ruby on Rails 2.3.5? (It encodes a string to be used in a query part of a URL)
I googled it but all the answers seem to date back to before 2006 and seems dates.
This is what I found. It seems a bit abnormal to call CGI::escape in a view.
Is there an equivalent helper function?
Thanks!

I believe the u helper method is what you're looking for:
<%=u "URL ENCODE <p>ME</p>" %>
This uses the method ERB::Util.url_encode, which is aliased to u.
You can find the documentation for this method here: http://rdoc.info/stdlib/erb/1.8.7/ERB/Util:url_encode.

If you want to do it without ERB, you can use the following:
Rack::Utils.escape('http://example.com')
#=> "http%3A%2F%2Fexample.com"
Which will also convert /

This worked better for me than the Rack::Utils.escape:
URI::escape('http://example.com/?param=Hello World')
Because it replaced the spaces with %20 instead of +
But it won't replace /

ERB::Util.html_escape, which is aliased to h and ERB::Util.url_encode, which is aliased to u .
http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB/Util.html
The method names seem to have changed since Sam Soffes answer, but the aliases haven't.

Related

Lua & Lighttpd - $_GET equivalent

What's Lua's equivalent to php's $_GET for a web application?
Also if the url is something like index.cgi?thisisatest how can I get everything after the question mark?
In the context of lighttpd and mod_magnet, query strings are not parsed automatically so you need to do it yourself. You can find an example here, look for "flv-streaming.lua" in the page.
As for your second question, Lorenzo gave you a generic answer, but in mod_magnet you can also use lighty.env["uri.query"] as seen in the same example.
Lua in itself is not a language for web development. There are some libraries for that. You can try luasocket.
As for your second question:
local url = "index.cgi?thisisatest"
local suffix = string.match( url, "^[^?]+?([^?]-)$" )
print( suffix )
If you are running your Lua code in Ophal, then you can use the functions: request_uri() and request_path()
If you're using lua as a CGI script, os.getenv("QUERY_STRING") will return everything after the question mark/

How can I insert a double quote in the session relevance

target fixlet name = abcdef"123"
Then how can I use the session relevance to get the record?
Name of fixlet contains "abcdef"123"" << doesn't work.
BTW DONT TRY TO EDIT ANYMORE, THIS IS NOT RELEVANT TO JAVA
IF YOU DONT KNOW WHAT IS SESSION RELEVANCE, PLEASE JUST SKIP THIS.
IBM relevance
Since you are outputting to the Web as HTML I guess you have to encode them into UTF-8.
Double quote is %22, according to Wikipedia, so try: "adcdef%22123%22"
Have you tried escaping quotes with backslash?

Is there an equivalent in RoR for PHP's gmdate?

For instance, if I called:
gmdate("M-D-yTh:i:s")
Is there something similar for this case in RoR? I guess I could always DateTime.now.hour, DateTime.now.year, etc. etc. but that seems extremely wrong.
See strftime() in Ruby's documentation on Time, which formats a time according to the directives in the given format string.

Find all URL's in a string in rails 3

I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html

Regular expression not working when put in an object

I'm trying to store regexes in a database but they're not working when used in a .sub(), even though the same regex works when used directly in .sub() as a string.
regex = Class.object.field // Class.object is an active record containing "\w*\s\/\s"
mystring = "first / second"
mystring.sub(/#{regex}/, '')
// => nil
mystring.sub(/\w*\s\/\s/, '')
// => second
Any insight appreciated!
Thanks,
Matt.
Editing to correct class/object terminology (thanks) & correcting my 2nd example as I had shown #{} wrapped around the working regex (cut & paste SNAFU).
To answer your question: It is not quite what kind of thing your Class.object is. If it's an ActiveRecord, it won't work.
Edit: You obviously found that the problem is Rails escaping the regexp.
An ActiveRecord cannot "contain" your regular expression directly; the regexp will be in one of the fields of your record. In which case you'd want to do something like regexp = Class.object.field_containing_the_regexp.
Even if that is not the case, I suspect that the problem is that your regexp is something other than a string. You can quickly test this by using
puts "My regexp: #{regexp}"
The string that you will see in the output will be the one that is used for the regexp.
A String is not a Regexp. You have to create a Regexp object first.
regex = Regexp.new("\w*\s\/\s")
Turns out my regexp didn't cater for all cases - \w didn't account for symbols. After checking in rails console, and seeing the screwey escaping I was alreasdy half-way down the wrong track.
Thanks for the help.

Resources