I need to add a variable in a translation key in a view of a Ruby on Rails project (not in the value, in the key). Ex, this is my key:
= t 'services.categories.website_html'
What I need to do is that the word "website" from that key comes from a variable named "category.className"
I have tryed this, with no results:
= t 'services.categories.#{category.className}_html'
Thanks in advance.
Use double quotes instead of simple quotes ;)
= t "services.categories.#{category.className}_html"
Strings are not interpolated inside single quotes, but they are in double quotes.
Related
in this gist
https://gist.github.com/greedo/957ba26575b3f5e445dc
there is a comments.coffee file.
in that it says
#accessor 'quote', ->
"“#{#get('current_comment')?.body}”"
The alternate type of double quotes is used. Is that on purpose? What are those called, and what is it doing there? Or is this just some character set conversion error. Tried to search but i have no idea what backwards double quotes are called.
Check out #mudasobwa's answer on this question How do I declare a string with both single and double quotes in YAML?
The main purpose of those quotes is so that it doesn't collide with the standard double quotes if dealing with a string that needs to output one. The coder can get away with having to remember to add a \ if he makes sure that every string that needs a double quote uses “ instead of ".
The code may be changed to the following without any effect.
#accessor 'quote', -> "\"#{#get('current_comment')?.body}\""
["{\"id\":317277848652099585,\"Tweet text\":\"Food Carnival by KMS Homemade Food\\nOn the occasion of this Holi and Good Friday, KMS Homemade Foods invites you... http://t.co/2Y2mO6vr76\",\"Word count\":21,\"Url\":\"true\"}"]
a is a hash with some keys and values.
a = a.to_json
converts the hash to a string.
Now a is a string with all backslashes...
I know tha
puts a
returns a string with all backslashes removed but what if i want to store the 'backslash removed string' in a variable?
It's much better to use as_json instead. Provides the json without backslashes.
For reference,
http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
You can just gsub! to replace the \" with a single quote ', like so:
a.gsub!(/\"/, '\'')
I faced the same problem and I did eval(a) and it gave me actual hash with no double quotes and slashes although eval is considered security risk
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(...)
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.
What is this?
"#{h params[:chat_input]}"
I am referring to the hash # and the h.
Most likely this is inside a double-quoted string, such as "Ponies all love #{h params[:chat_input]}!" The #{stuff} expression causes the stuff expression to be interpreted and inserted into the string. For example "1 + 2 = #{1 + 2}" will result in the string "1 + 2 = 3".
The h is an alias to the html_escape method, which is pretty self-explanatory.
The code you paste, by itself, is just a comment. I assume the code is inside a string, though.
"hello, #{5 + 5}"
# => hello, 10
The statement inside the brackets will be evaluated as Ruby. This is called string interpolation.
The statement inside the interpolation in your code is a method that gets an argument.
h params[:chat_input]
h(params[:chat_input])
The h method is a shortcut for html_escape, which escapes HTML. For example, <span> is converted into <span>, so that the browser displays the actual contents of the string, instead of interpreting it as HTML.
html_escape(params[:chat_input])
You probably know what params is.
To sum up, you get a HTML escaped version of whatever params[:chat_input] contains.
"#{h params[:chat_input]}"
In ruby, double-quoted strings allow for expressions to be evaluated and automatically converted to strings.
I can do this:
years = 25
"John is " + years + " years old"
but I'll get an error because I can't add the number to a string.
I can do
"John is #{years} years old"
to get around that.
The h() method is a Rails helper function that removes HTML tags. It's a safety thing.
Finally, params() is a method in Rails that gives you access to GET and POST parameters. It's actually wrapping a hash GET and POST parameters are symbolized to reduce memory (symbols are only defined once, whereas a string like "foo" is a new object every time.)
So, params[:chat_input] retrieves the value from the previous request's GET or POST parameters, and in your case it looks like it's just displaying and sanitizing them.
Hope that helps!
It's just interpolating a value inside a string. The :chat_input is a symbol, it's used in place of a string because symbols are only created once.
h(something)
or
h something
since ruby does not force the use of (), is a function available in rails that converts the parameter to a "safe HTML" string avoiding interpreting the possible HTML code inside of the 'something' variable.
"#{x}"
in ruby means converting the x variable to a string and placing it in the new string for example:
"#{host}:#{port}"
will place the value of host and the value of port into the new string formed by the "", in a way that if host is "localhost" and port is 30 the result string will be "localhost:30"
params is a special rails hash that contains the post/get parameters passed to the controller method being executed
another detail is that in ruby a method always returns the last evaluated expression
so the method
def test
"#{h params[:chat_input]}"
end
will return a string that has the HTML-safe value of the post/get parameter chat_input
holy crap, is that from chat_sandbox by any chance?
if so, let me know if you need any help $)
I'm hoping to update that code here soon.