Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6761 (closed defect: worksforme)

Opened 3 years ago

Last modified 6 months ago

text_field_tag ignores symbols

Reported by: jerdent Assigned to: David
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: text_field_tag
Cc:

Description

Hi

For a given hash @foo { :bar => "pim", :soap => "poum" }, I found that

text_field_tag( "foo", @foo[:bar] ) won't work but text_field_tag( "foo", @foobar? ) will.

I cannot assign the "value" argument when accessing a hash with a symbol but a string.

Is it a bug or something ?

Tested with today's rails edge.

Change History

01/11/07 03:42:12 changed by dcmanges

  • status changed from new to closed.
  • resolution set to worksforme.

Are you doing anything else with the @foo hash that might be modifying it?

The "value" passed to text_field_tag is passed as normal in Ruby; the text_field_tag method does not know how it is being passed in. Take a look at the current implementation:

def text_field_tag(name, value = nil, options = {})
  tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
end

12/23/08 07:30:08 changed by smarkwell

So I was able to create a situation similar to what's described above in Rails 2.2.2

In a helper I was doing HTML generation. Passed into the method was a hash representing the individual item to generate HTML for, this hash was populated from a hash stored in the clients session (part of a wizard). The helper is called from erb.

I suspect the helper is not loading the Rails modification to symbols that auto-magically convert them to/from strings when needed. Thus inside the helper

text_field_tag("test" , item["price"]) # works!
text_field_tag("test" , item[:price]) # fails!

The work around I'm using is simply not use symbols in this case, even though everywhere else in the code base is perfectly happy using symbols and this is how they are populated and retrieved.