1

In a Rails app, I've run into a case where I'd like to have a checkbox name that ends in square brackets, e.g.:

name="foo[bar][baz[]]"

Other special characters seem to be handled correctly, but it looks like Rails is stripping out the square brackets and treating them as declaring an array rather than being part of the name. What needs to be done to allow arbitrary characters (brackets in particular) in this name and have them be processed correctly by Rails?

jrdioko
  • 32,230
  • 28
  • 81
  • 120
  • If you want to have a collection of elements assigned to variable you're looking for `name="foo[bar][baz][]"` – Mike Szyndel Sep 24 '13 at 18:25
  • Can you explain a little more about what are you trying to do? – Mike Szyndel Sep 24 '13 at 18:28
  • I'm trying to support all characters (I guess other than spaces, as prohibited by the HTML5 spec) in the name of a checkbox. I noticed that when I try to name one `baz[]` it isn't handled correctly because of the Rails logic regarding square brackets. I'm wondering if there's a way to escape brackets (and other special characters?) so that they pass through correctly. – jrdioko Sep 24 '13 at 18:32
  • No. Don't tell me about your programming solution because it's clearly wrong. Show me the code and explain what are you trying to get as a result in simple words. – Mike Szyndel Sep 24 '13 at 18:35
  • The code is ``. I'm expecting `params` to include a `foo` key of `{"bar"=>{"baz[]"=>"1"}}`. Instead, the value for that key is `{"bar"=>{"baz"=>[nil]}}`. – jrdioko Sep 24 '13 at 18:51
  • So it is a group of checkboxes, right? There's no such thing in HTML as `foo[bar][baz[]]` – Mike Szyndel Sep 24 '13 at 19:48
  • Yep, it's a group of checkboxes (called `bar`) and I'd like one of them to be named `baz[]`. The obvious answer is to not choose such a ridiculous name, but these are (trusted admin) user-generated strings and I wanted to know if it was possible (through escaping or something else). Are you saying the HTML is semantically invalid? – jrdioko Sep 24 '13 at 21:23
  • Yes, they are invalid. Initially I thought you want to have a group of checkboxes, but now I fully understand. This is a "limitation" of HTNL and I think you should avoid using such a name. I see no real life reason for that as well. – Mike Szyndel Sep 25 '13 at 10:48

2 Answers2

1

That's because the square brackets are not allowed in the specification:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

from HTML 4 Basic HTML data types

About the Rails part, and combining these two answers (1 and 2):

Rails make use of square brackets to make associations in the params Hash, so you shouldn't mess with those names.

Community
  • 1
  • 1
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • I'm using HTML5, which I believe is more permissive. And Rails already uses brackets as part of the framework (which is interesting if it's breaking the specification). – jrdioko Sep 24 '13 at 18:09
  • First of all you're totally wrong. A collection of checkboxes would be described (almost) exactly as OP wrote. – Mike Szyndel Sep 24 '13 at 18:24
  • 1
    Ok, after some back and forth you answer is totally ok, up voted. – Mike Szyndel Sep 25 '13 at 10:49
0

If you really need to, you can CGI.escape the values you don't want parsed automatically. You'll then have to CGI.unescape them on the other side.

"foo[bar][#{CGI.escape(name)}]"
CGI.unescape(params[:foo][:bar])
lobati
  • 9,284
  • 5
  • 40
  • 61