English 中文(简体)
铁道纽拉角
原标题:Ruby on Rails button click to populate text_field using jQuery

我试图用 j子把一个 text子点击,并提出一些问题。 我在此提出了这个问题,但我对所提供的答案持怀疑态度,因为答案似乎过于复杂(这可能是正确的,但我没有执行过。

Ruby on Railways jQuery button Pointpope text_field

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.

更引人注意这个问题......

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. Am I correct in using a button_tag?
  2. Where should the javascript file be located, I ve read a bit about this and some mention creating an edit.js.erb file and have the controller also respond to javascript. I am a bit unsure about this process however and as I need similar functionality in the new and edit pages, one global jQuery function would be better.
  3. If calling a method from the controller through jQuery is right way could someone please provide some sample code of how this is done as I had no luck when trying. If it is possible to call SecureRandom.urlsafe_base64 from a js.erb file and have the field update each time the button is clicked that would be ideal.

正如在另一个问题上看到的那样,我做的是一件事,但现在却步不前,而且变得非常绝望。

I would be grateful for any help and sample code.

亲爱!

最佳回答

我假定你已经 j了,并且已经做了正确的工作。 尽管如此,为了简单起见,将这一javascript in an app/assets/javascripts/devices.js. 如果是的话,确保你提出申请。 j 在同一名录中包含“/要求......”(该目录的所有档案,包括你刚创建的档案) 最重要的是,确保你将“申请......js”列入正在作出的布局。 (例如,见/views/layouts/application.html.erb)

app/assets/javascripts/devices.js:

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

之后,加上这条路线,美国宇宙航空研究开发机构呼吁了解什么控制者和行动叫......

页: 1

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

接着,增加控制者的行动,把“安全多姆”方法称为你要求......

• 用户/管理人员/用户

def generate_device_id
 render :text=>SecureRandom.urlsafe_base64
end
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签