passing variable to a string in a Rails controller - ruby-on-rails

I need to pass a variable into a string like this.
HTTParty.get(https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false)
This works fine as a string, but I cant seem to get the syntax correct to make the parameters variables. Can someone please help me with the syntax.

It's not clear what issue you're having (or if the missing quotes are just a typo). Normal string interpolation should do what you want:
bar = "123.45"
plugh = "xyz"
HTTPart.get("blahblah?foo=#{bar}&baz=#{plugh}")
You'll want to url-encode the values as well, most likely, unless they're under control.

Related

Compare variable to text disregarding caps [duplicate]

Here is the thing. I am trying to convert a string in lowercase in Lua, but it's not working. I have done this
String = String:lower()
but it doesn't like it. I am sure that is the way to do it, I've seen it done before. A few sites suggest it might be a problem caused by a wrong version of the interpreter.
Any ideas?
You're right, this is one of the ways to do it. It would only not work and throw errors if your "String" variable is not a string.
Personally, i usually prefer to use something like..
myString = string.lower(myString)
But its really the same as doing
myString = myString:lower()
assuming that myString is actually a string, however.
The "long" version has one advantage, it actually works if myString is a number, while the second one errors in that case.

To convert variable name to string or symbol

it's possible in rails convert variable name to string?
Example:
stack_overflow = "Fantastic!".
by stack_overflow.to_sym I'll get :Fantastic!.
Instead I would => :stack_overflow
and, if it were possibile, this would be bad programming cause unsafe code?
Thanks :-)
You'd need to use eval(), although I don't know the specific way you'd achieve what you want:
new_var = eval(stack_overflow.to_sym)

URL Encode string for Href ASP.NET MVC / Razor

I'm trying to build an Href using Razor
The string is going to end up looking like this:
https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D
In my model I have the order id and the hash string
As the route is not a part of my site I don't believe I can use the default methods like #Url.Action
and therefore can't use protocol: Request.Url.Scheme
like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions
I've tried
Url.Encode
Url.EscapeDataString
Html.Encode
but am getting no where fast:
Click Here to be transferred
The output text always has plusses and equals in them and doesn't work.
Which combination do I need?!
I've figured out a way of doing it:
#{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
#Uri.EscapeDataString(Model.bookingNumber.ToString()),
#Uri.EscapeDataString(Model.hashCode));
}
<p>Click Here to be transferred</p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.
This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.
System.Net.WebUtility.UrlEncode(MyVariableName)
If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.
The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.
You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.
Ultimately, it would look something like this:
Click Here to be transferred
The easier method is to use #Html.Raw(Model.SomethingUrl)

How to add prefix and remove double quotes from the value in ruby on rails?

I have a value called "FooBar". I want to replace this text with the quotes to Enr::Rds::FooBar without quotes.
Update:
For example, #question.answer_model gives the value "FooBar" (with the quotes)
I am a newbie and somebody please refer me how would i start to know about regex? What is the best way to practice in online?
Since you want to: a) drop the quotes and b) prepend FooBar with Enr::Rds::, I would suggest you preform exactly what is intended, literally:
'"FooBar"'.delete('"').gsub(/\A/, "Enr::Rds::")
# => "Enr::Rds::FooBar"
I think you are trying to convert a string to a constant. try the following
"Enr::Rds::#{#question.answer_model.gsub('"', '')}".constantize.find(...)

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

Resources