Forcing helper like link_to to use single quotes

For example, I want <a href='link'> rather than <a href="link">

I want to maintain consistency with style.

Can this be done?

Also, I am trying to use Rails 3.0.

Sharkie

You want to change the correct (according to the xhtml guidelines)
double-quotes, to incorrect single quotes?

Wouldn't it be better to change the single quotes to double? (For
compliance, rather than consistence).

Sharkie Landshark wrote:

For example, I want <a href='link'> rather than <a href="link">

I want to maintain consistency with style.

Can this be done?

Also, I am trying to use Rails 3.0.

Sharkie

I would not recommend using single quotations over double. One argument
against using single quotes is that you will not be able to embed ruby
code into your link:

"<br />A link to a #{link_to("website", "http://ruby-forum.com")}"

If you want consistency, consistently use double quotations.

Pale Horse wrote:

Sharkie Landshark wrote:

For example, I want <a href='link'> rather than <a href="link">

I want to maintain consistency with style.

Can this be done?

Also, I am trying to use Rails 3.0.

Sharkie

I would not recommend using single quotations over double. One argument
against using single quotes is that you will not be able to embed ruby
code into your link:

"<br />A link to a #{link_to("website", "http://ruby-forum.com")}"

That's not true. The OP seems to be talking about the quotation marks
in the generate HTML, not in the Ruby source code.

If you want consistency, consistently use double quotations.

Better yet, don't worry about it.

Best,

Marnen Laibow-Koser wrote:

That's not true. The OP seems to be talking about the quotation marks
in the generate HTML, not in the Ruby source code.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

It would appear I've been misguided.

Always using double quotes causes every string to be interpolated,
often when not required. Single quoted string are not interpolated,
saving CPU cycles. Computer science 101.

Looking at the original post it appears he is talking about
single/double quotes in the html, not in Ruby code. Interpolation is
therefore not occurring.

Colin

Dear Friends,

I am the original poster. I use double quote for all Ruby strings in my
Ruby codes.

But, I use single quotes for HTML attributes.

How would I force such behavior even though it is not compliance etc.

Sheesh, I don't know...

Smack yourself in the head every time you type a double-quote for an
HTML attribute???

Use an IDE and re-write the syntax rules for the language recognition so
it does not allow double-quotes for HTML attributes?

Surely, there are more important things you could be learning in the
time you are spending trying to force the code to be 'just so'.

Regardless of sharkie's original intent, there's a use case where
forcing single quotes on an HTML attribute value, generated through a
Rails helper, would be useful - namely attaching JSON to HTML elements
via HTML 5 data attributes. In order for the JSON to be valid, it needs
to be wrapped in single quotes so that double quotes can be utilized
within the JSON itself, as required in the JSON spec. Given that single
quotes on attribute values are permissible within the HTML spec, I see
generating HTML through a Rails helper that uses single, rather than
double, quotes as a reasonable request. Unfortunately, I have yet to
figure out how. Any ideas would be greatly appreciated.

Greg Leppert wrote in post #961244:

Regardless of sharkie's original intent, there's a use case where
forcing single quotes on an HTML attribute value, generated through a
Rails helper, would be useful - namely attaching JSON to HTML elements
via HTML 5 data attributes. In order for the JSON to be valid, it needs
to be wrapped in single quotes so that double quotes can be utilized
within the JSON itself, as required in the JSON spec. Given that single
quotes on attribute values are permissible within the HTML spec, I see
generating HTML through a Rails helper that uses single, rather than
double, quotes as a reasonable request. Unfortunately, I have yet to
figure out how. Any ideas would be greatly appreciated.

I think that HTML-escaping the double quotes in the JSON (i.e.
converting to &quot;) would do the trick. Try it and see.

Best,

Thanks for the response Marnen. Unfortunately, I don't think that'll
work. Here's, for instance, what the Webkit Javascript console returns
using the native JSON.parse method:

JSON.parse("{&quot;test&quot;:100}");

SyntaxError: Unable to parse JSON string

JSON.parse("{'test':100}");

SyntaxError: Unable to parse JSON string

JSON.parse('{"test":100}');

Greg Leppert wrote in post #961266:

Thanks for the response Marnen. Unfortunately, I don't think that'll
work. Here's, for instance, what the Webkit Javascript console returns
using the native JSON.parse method:

JSON.parse("{&quot;test&quot;:100}");

SyntaxError: Unable to parse JSON string

JSON.parse("{'test':100}");

SyntaxError: Unable to parse JSON string

JSON.parse('{"test":100}');

Object

Those tests are irrelevant: by the time the &quot; gets to the JS
parser, it will be a double quote, I think. The entity is only for
serialization; it will be a " in the parses DOM that JS sees.

If you want to test this properly, you need some actual HTML. Try that.

Best,

by the time the &quot; gets to the JS parser, it will be a double quote, I think

Nope, should have mentioned that in the last email.

I stand corrected. I was improperly escaping the quotes when I tested a
helper method the first time around. The JSON from my previous example
parses correctly when added like so:
"data-mydatakey" => "{\"test\":100}"

Marnen, sorry for jumping to conclusions and thanks for the help.