mko
mko

Reputation: 22064

store string with entities in rails variable

hey guys when I store this string:

http://61.147.96.19/f4v/41/53283141.h264_2.f4v?10000&key=d7d7488b5dd4a35aac3e784cf4acb1a174ddc7&playtype=1&tk=2038443745&brt=3&id=tudou&itemid=30165756&fi=53283141&sz=19354294

in a variable @longurl

and use it in a erb file

<%= @longurl %>

It doesn't work, when I check the source file in the browser :

http://61.147.96.19/f4v/41/53283141.h264_2.f4v?10000&amp;key=d7d7488b5dd4a35aac3e784cf4acb1a174ddc7&amp;amp;playtype=1&amp;amp;tk=2038443745&amp;amp;brt=3&amp;amp;id=tudou&amp;amp;itemid=30165756&amp;amp;fi=53283141&amp;amp;sz=19354294

and compare the differece, it add a amp; after each &amp, and I don't know how to avoid this.

BTW it seems @url is taken by the rails by default use, I can't save anything in the variable, Could anyone tell me why?

Thanks

Upvotes: 0

Views: 636

Answers (4)

dontangg
dontangg

Reputation: 4809

Before Rails 3, you used to have to code like this to make sure everything is HTML encoded:

<%= h @longurl %>

The h method would HTML encode your string. Now, everything is HTML encoded by default. If you don't want Rails to do this for you, we now have the raw method like this:

<%= raw @longurl %>

Read more about the raw method here: http://api.rubyonrails.org/classes/ActionView/Helpers/RawOutputHelper.html#method-i-raw

or read about this change in the Rails 3 release notes http://edgeguides.rubyonrails.org/3_0_release_notes.html#other-changes

Upvotes: 1

Zach Inglis
Zach Inglis

Reputation: 1257

You actually want & Read why here: http://htmlhelp.com/tools/validator/problems.html

Is it 'breaking' the method in any other way, or just because it looks different?

If @url is used by Rails, use a different variable. It shouldn't be a deal breaker.

Upvotes: 1

Jacob Relkin
Jacob Relkin

Reputation: 163248

You can use CGI::unescape to decode a URL-encoded string.

<%= CGI::unescape(@longurl) %>

For HTML entities, use CGI::unescapeHTML:

<%= CGI::unescapeHTML(@longurl) %>

Upvotes: 1

encoded
encoded

Reputation: 1174

Assuming you're using Rails 3, try <%= @longurl.html_safe %>.

Upvotes: 1

Related Questions