I have this in my controller:
@text = params[:data][:text]
In the view, I have this code:
<%= label_tag('data[text]', "Word/phrase: ") %> <br />
<%= text_field_tag( 'data[text]', @text, :size => 40, :maxlength =>
254 ) %>
What I would like to have is to get a default value for the form
field (some text that the user previously entered). I cannot figure
out why @text controller instance variable doesn't work. It's a non-
empty string, but the corresponding form
field is always blank.
However, this piece of code always works,
<%= text_field_tag( 'data[text]', 'STATIC STRING', :size =>
40, :maxlength => 254 ) %>
Are you sure @text contains something? What do you see if you include
something like
<p><%= @text %></p>
before the text_field_tag.
Also have a look at the html of the page (View, Page Source or similar
in browser) and see what the field html looks like. If the value is
there in the html but does not appear on screen, copy all the page
html source and enter it into the w3c html validator at
The W3C Markup Validation Service and see if there are html
errors which could be confusing the browser.
Are you sure @text contains something? What do you see if you include
something like
<p><%= @text %></p>
before the text_field_tag.
I see an empty paragraph.
Also have a look at the html of the page (View, Page Source or similar
in browser) and see what the field html looks like. If the value is
there in the html but does not appear on screen, copy all the page
html source and enter it into the w3c html validator athttp://validator.w3.org/#validate_by_inputand see if there are html
errors which could be confusing the browser.
It did NOT validate. This is one of the errors,
Line 59, Column 31: character "[" is not allowed in the value of
attribute "id"
<textarea cols="40" id="data[comment]" name="data[comment]"
rows="10"></textar
It is possible that you violated the naming convention for this
attribute. For example, id and name attributes must begin with a
letter, not a digit.
I will dwell on this issue later. Right not I have to catch the
bus. I will one modification to the code and see what's gonna
happen.
Are you sure @text contains something? What do you see if you include
something like
<p><%= @text %></p>
before the text_field_tag.
I see an empty paragraph.
In that case @text is empty, so that is the problem to be fixed.
Unless the error below is causing it to show empty which does not seem
likely.
If you have trouble working out why @text is empty it may be worth
while having a look at the RoR guide on Debugging at
http://guides.rubyonrails.org. I find ruby-debug very useful when
code inspection does not reveal the problem.
Also have a look at the html of the page (View, Page Source or similar
in browser) and see what the field html looks like. If the value is
there in the html but does not appear on screen, copy all the page
html source and enter it into the w3c html validator athttp://validator.w3.org/#validate_by_inputand see if there are html
errors which could be confusing the browser.
It did NOT validate. This is one of the errors,
Line 59, Column 31: character "[" is not allowed in the value of
attribute "id"
<textarea cols="40" id="data[comment]" name="data[comment]"
rows="10"></textar
It is possible that you violated the naming convention for this
attribute. For example, id and name attributes must begin with a
letter, not a digit.