default to text in a search for with ruby and rails
I want to have a text value for a form like "search website.com..." but if the form has been submitted i want the query to appear- i dont know ruby idioms that 开发者_C百科well but i was think
<%= text_field_tag "q", params[:q] | "search website.com...." %>
is this correct?
That will work, but you should use ||
(logical OR operator) instead of |
(bitwise OR).
That should work pretty well. The only thing I might add is that you should use a bit of javascript to check if it is the "default" string of "search website.com..." and if so, when a user clicks or focuses on the text box, you should clear the default string so that they don't have to do it themselves. Again, this should only be done on the default string, not if the user enters one.
An alternative approach could be using the :placeholder instead. Something like this:
= text_field_tag :q, params[:q], :placeholder => "Search website.com …".html_safe
Side Note: .html_safe lets you use the html code for ellipses instead of using three dots
精彩评论