简体   繁体   English

默认为使用ruby和rails搜索的文本

[英]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 well but i was think 我想为诸如“ search website.com ...”之类的表单提供文本值,但是如果表单已提交,我希望查询出现-我不知道红宝石成语,但我认为

<%= 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. 我可能要添加的唯一一件事是,您应该使用一些JavaScript来检查它是否是“ search website.com ...”的“默认”字符串,如果是,则当用户单击或关注文本框时,您应该清除默认字符串,以便他们不必自己做。 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. 另一种方法是使用:placeholder代替。 Something like this: 像这样:

= text_field_tag :q, params[:q], :placeholder => "Search website.com &#8230;".html_safe

Side Note: .html_safe lets you use the html code for ellipses instead of using three dots 旁注 .html_safe可让您将html代码用于省略号,而不是三个点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM