Help interpret this bit of Rails code

What is it?

"#{h params[:chat_input]}"

      

I mean hash #

and h

.

+1


a source to share


6 answers


Most likely it is inside a double quoted string, for example "Ponies all love #{h params[:chat_input]}!"

. The expression #{stuff}

causes the expression of the element to be interpreted and added to the string. For example "1 + 2 = #{1 + 2}"

will result in a string "1 + 2 = 3"

.



h

is a method alias html_escape

which is pretty self explanatory.

+8


a source


The code you insert is itself just a comment. I am assuming the code is inside a string.

"hello, #{5 + 5}"
# => hello, 10

      

The statement inside the brackets will evaluate to Ruby. This is called string interpolation.

The assertion inside interpolation in your code is the method that takes the argument.

h params[:chat_input]
h(params[:chat_input])

      



Method h

is a shortcut to html_escape

which speeds up HTML. For example, it is <span>

converted to &lt;span&gt;

so that the browser displays the actual content of the string instead of interpreting it as HTML.

html_escape(params[:chat_input])

      

You probably know what it is params

.

To summarize, you get the HTML escaped version of any parameters [: chat_input].

+4


a source


"# {h params [: chat_input]}"

In ruby, double-quoted strings let you evaluate expressions and automatically convert them to strings.

I can do it:

years = 25
"John is " + years +  " years old"  

      

but I get the error because I cannot add the number to the string.

I can do

"John is #{years} years old"

      

to get around this.

The h () method is a Rails helper function that removes HTML tags. It's a security thing.

Finally, params () is a method in Rails that gives you access to GET and POST parameters. In fact, it wraps the GET and POST hash parameters, which symbolize memory reduction (characters are only defined once, whereas a string like "foo" is a new object every time.)

So params [: chat_input] fetches the value from the previous GET or POST request parameters, and in your case it looks like it's just displaying and sanitizing them.

Hope it helps!

+4


a source


It just interpolates the value within the string. User: chat_input is a character that is used instead of a string because characters are only created once.

+1


a source


h(something)

      

or

h something

      

since ruby ​​does not enforce (), is a function available in rails that converts the parameter to a "safe HTML" string, avoiding the interpretation of possible HTML code within the "something" variable.

"#{x}"

      

in ruby ​​means converting the variable x to a string and placing it on a new line like:

"#{host}:#{port}"

      

will put the host value and port value on a new line formed by ", so if the host is" localhost "and the port is 30, the result is" localhost: 30 "

params - a special hash rails that contains post / get parameters passed to the controller method being executed

another detail is that in ruby ​​the method always returns the last evaluated expression

so the method

def test
   "#{h params[:chat_input]}"
end

      

will return a string that has an HTML-safe value for the post / get chat_input parameter

+1


a source


Holy crap, is this from chat_sandbox , if possible?

if yes let me know if you need help $)

I hope to update this code soon.

0


a source







All Articles