簡體   English   中英

設置文本的值是一個隨機數,是字符和整數的組合?

[英]Set value of text a random number a combination of character and integer?

這是結合integercharacter的隨機數的代碼

<%=(0...4).map { ('a'..'z').to_a[rand(26)] }.join %> <%=rand(1..2000)%>

給出如下輸出:

tqzu 1664

但是當我把它放入文本框時

<input type = "text" value=<%=(0...4).map { ('a'..'z').to_a[rand(26)] }.join %><%=rand(1..2000)%>  />

輸出值

ztgg

題:-

how to get the value of random number into textbox with the combination of char and integer

它僅處理第一部分。 如果將代碼放在幫助程序中,則可以在視圖內調用該幫助程序方法,並且不會出現任何問題。

您必須將代碼放入"

<input type = "text" value="<%=(0...4).map { ('a'..'z').to_a[rand(26)] }.join %> <%=rand(1..2000)%>" />

或更好:

# in helper
def code
  [(0...4).map { ('a'..'z').to_a[rand(26)] }.join,
   rand(1..2000)].join(' ')
end

# in view
<%= tag :input, :value => code %>

在您的表單中放置這樣的代碼

<%=@rand_value = (0...4).map { ('a'..'z').to_a[rand(26)] }.join.to_s + rand(1..2000).to_s %>
<%= f.text_field :text, :value => @rand_value %>

或者您可以按以下方式使用text_field_tag:

<%= text_field_tag "text", @rand_value %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM