Ruby on Rails的jQuery的按钮,然后单击填充text_field然后单击、按钮、on、Ruby

2023-09-10 18:57:54 作者:谁都不了解谁i

我有以下形式我edit.html.erb页

I have the following form in my edit.html.erb page

<%= form_for(@device) do |f| %>

  <%= render 'shared/error_messages', :object => f.object %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :description %>
  <%= f.text_area :description, :rows => "5" %>

  <%= f.label :device_identifier %>
  <%= f.text_field :device_identifier, :value => @device.device_identifier, :readonly => true, :class => "d-id", :id => "deviceIDfield" %>


  <%= button_tag "New device identifier", :id => "deviceIDbutton", :class => "btn btn-small btn-inverse", :type => "button" %>

  <%= f.submit "Save changes", :class => "btn btn-large btn-primary", :id => "white" %>
<% end %>

在新建设备标识符我想一下:device_identifier领域的再生是目前通过Ruby的方法SecureRandom.urlsafe_base64设定值。这将生成一个随机的base64连接codeD字符串。我一直在努力,因为我是新来的两个Ruby on Rails和jQuery来做到这一点通过的jQuery但时遇到了问题。

When clicking on the "New device identifier" I want the :device_identifier field to regenerate its value which is currently set through the Ruby method SecureRandom.urlsafe_base64. This simply generates a random base64 encoded string. I have been trying to do this through jQuery however am having trouble as I am new to both Ruby on Rails and JQuery.

我成立了一个名为shared.js.erb它位于我的javascripts目录下的文件。在这里,我有以下code ...

I set up a file called shared.js.erb which is located in my javascripts directory. In here I have the following code...

$(document).ready(function(){
 $('#deviceIDbutton').click(function () {
     $("#deviceIDfield").val('<%= SecureRandom.urlsafe_base64 %>');
     });
})

这工作我第一次点击的按钮(也就是它更新device_identifier领域的一个新的字符串),在此之后却停止工作。我想这是因为SecureRandom.urlsafe_base64被调用一次(当文件已准备就绪..?),而不是每次点击的按钮。可能有人请帮助我!我一直在敲打我的头了本作的时间太长了。

This works the first time I click the button (i.e. it updates the device_identifier field with a new string) however stops working after this. I assume this is because SecureRandom.urlsafe_base64 is being called once (when the document is ready..?) and not each time the button is clicked. Could someone please help me!, I've been banging my head over this for way too long.

我相当不确定的几件事,如为什么我不需要使用:远程=>真正的任何地方,如果我用它在哪里把它

I am quite unsure about a few things such as why I don't need to use :remote => true anywhere and if I were to use it where to put it?

另外,我不能肯定我有什么地方jQuery的应该去,虽然这似乎是我创建的shared.js.erb文件的工作,如果有一个人能推荐一个更好/更正确的观念这将是伟大的自然之道。

Also, I am by no means sure that I have the right idea about where the jQuery should go and although it seems to work in the shared.js.erb file that I created, if some one could recommend a better/more natural way that would be great.

另外值得一提的是,我在我的new.html.erb文件相同的按钮,我想申请相同的功能。

It's also worth mentioning that I have the same button in my new.html.erb file which I want to apply the same functionality.

非常感谢!

推荐答案

正如你所说的,随机的字符串正在生成一次,而不是在该文件已准备就绪。通过您的布局包括时shared.js.erb被呈现在服务器端。这种创造的内容shared.js文件,如:

As you said, the random string is being generated only once, but not when the document is ready. The shared.js.erb is being rendered in the server side when included by your layout. This 'creates' a shared.js file with the content, for eg.:

$(document).ready(function(){
 $('#deviceIDbutton').click(function () {
     $("#deviceIDfield").val('werwe87wer78we7w8erew');
     });
})

在就绪()调用的功能正在被执行时,文档准备好和结合上的单击事件的#deviceIDbutton元件所要执行的功能。所以,每次您点击按钮,在执行同样的功能,一次又一次地设定相同的随机code。

The function in the ready() call is being executed when the document is ready and that binds a function to be executed on the click event to the #deviceIDbutton element. So, every time you click your button, the same function in executed, setting the same random code again and again.

您功能应该是建立远程调用的返回一个新的安全随机code控制器的方法。为此,您可以使用JQuery.get()。

You function should be making a remote call to a method in the controller that returns a new secure random code. You can do this using JQuery.get().

如果在code只是将是一个随机生成的code,你可以与一些JavaScript的浏览器生成,避免了调用服务器端。

If the code is just going to be a randomly generated code, you could generate on the browser with some JavaScript, avoiding the call to the server side.

您可以看到this其他SO回答调用控制器方法的例子。

You can see this other SO answer for examples of calling a controller method.

 
精彩推荐
图片推荐