Ruby on Rails按钮单击以使用jQuery填充text_field [英] Ruby on Rails button click to populate text_field using jQuery

查看:157
本文介绍了Ruby on Rails按钮单击以使用jQuery填充text_field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery单击按钮来填充text_field,并且有几个问题.我在这里发布了一个与此有关的问题,但不确定所提供的答案,因为它似乎过于复杂(这可能是正确的,但我对实施没有运气)...

I am trying to populate a text_field on click of a button using jQuery and have a few questions. I posted a question about this here but wasn't sure about the answer provided as it seemed overly complicated (It could be the correct but I had no luck with the implementation)...

Ruby on Rails jQuery按钮单击以填充text_field

以基督教徒在另一个问题中提到的方式或简单的方式进行此操作的确认会很棒,并且示例代码将非常受赞赏.如另一个问题所述,我对ROR和jQuery都是新手.

Confirmation of doing it the way Christian mention in the other question or something simpler would be great, and sample code would be greatly appreciated. As mentioned in the other question I am very new to both ROR and jQuery.

有关该问题的更多信息...

Some more info about the question...

该按钮在new和edit.html.erb中都退出,并且位于text_field旁边的form_for块中(我有一个单独的按钮来提交表单).我希望能够使用Ruby方法SecureRandom.urlsafe_base64填充字段.

The button exits in both the new and edit.html.erb and is located in a form_for block next to a text_field (I have a separate button to submit the form). I want to be able to populate the field using the Ruby method SecureRandom.urlsafe_base64.

  1. 我可以正确使用button_tag吗?
  2. 应该将javascript文件放在哪里,我已经阅读了一些有关此的内容,并提到了创建edit.js.erb文件并使控制器也响应javascript.但是我对此过程并不确定,因为在新建页面和编辑页面中我需要类似的功能,所以最好使用一个全局jQuery函数.
  3. 如果通过jQuery从控制器中调用方法是正确的方法,那么有人可以提供一些示例代码来完成此操作,因为尝试时我没有运气.如果可以从js.erb文件中调用SecureRandom.urlsafe_base64,并且每次单击按钮时都需要更新字段,那么这是理想的选择.

从另一个问题中可以看出,我可以正常工作,但是现在陷入困境,并且变得非常绝望.

As can be seen in the other question I got something working but am now stuck and it's becoming quite desperate.

如果有任何帮助和示例代码,我将不胜感激.

I would be grateful for any help and sample code.

一吨!

推荐答案

我假设您已经包含jQuery并且已经可以正常工作.话虽如此,为简单起见,请将此javascript放入app/assets/javascripts/devices.js.如果这样做,请确保同一目录中的application.js包含"//= require_tree"(它将拉入该目录中的所有文件,包括刚刚创建的文件.)首先,请确保您包括呈现的布局中的"application.js". (可能是app/views/layouts/application.html.erb)

I'm assuming you have jQuery included and working correctly already. With that said and for simplicity, put this javascript in an app/assets/javascripts/devices.js. If you do so, ensure your application.js in the same directory includes a "//=require_tree . " (which will pull in all the files in that directory including the one you just created.) Above all, ensure you are including "application.js" in the layout that's being rendered. (likely app/views/layouts/application.html.erb)

app/assets/javascripts/devices.js:

app/assets/javascripts/devices.js:

$(function(){
  $("#deviceIDbutton").click(function(e){
      $.get("/generate_device_id", function(data){
      $("#deviceIDfield").val(data);
    });
    e.preventDefault();
  })
})

然后,添加路由,以便AJAX调用知道要调用的控制器和动作...

Then, add the route so the AJAX call knows what controller and action to call...

app/config/routes.rb

app/config/routes.rb

get '/generate_device_id', :to=>"devices#generate_device_id"

然后,添加控制器操作,该操作将调用您要求的SecureRandom方法...

Then, add the controller action that will call the SecureRandom method you're asking for...

app/controllers/devices_controller.rb

app/controllers/devices_controller.rb

def generate_device_id
 render :text=>SecureRandom.urlsafe_base64
end

这篇关于Ruby on Rails按钮单击以使用jQuery填充text_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆