简体   繁体   English

设置文本的值是一个随机数,是字符和整数的组合?

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

Here is the code for the random number combining integer and character 这是结合integercharacter的随机数的代码

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

Gives output like: 给出如下输出:

tqzu 1664

But when i put this into textbox like; 但是当我把它放入文本框时

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

OUTPUT 输出值

ztgg

Question:- 题:-

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

It only processes the first part. 它仅处理第一部分。 If you place your code in a helper you can call the helper method within your view and you shouldn't have any problems. 如果将代码放在帮助程序中,则可以在视图内调用该帮助程序方法,并且不会出现任何问题。

You have to put your code into " : 您必须将代码放入"

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

or better: 或更好:

# 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 %>

In your form place code like this 在您的表单中放置这样的代码

<%=@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 %>

or you can use text_field_tag as below: 或者您可以按以下方式使用text_field_tag:

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

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

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