title: Ruby on Rails button click to populate text_field using jQuery
body: <p>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)...</p>
  
  <p><a href="https://stackoverflow.com/questions/10229152/ruby-on-rails-jquery-button-click-populate-text-field">Ruby on Rails jQuery button click populate text_field</a></p>
  
  <p>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.</p>
  
  <p>Some more info about the question...</p>
  
  <p>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.</p>
  
  <ol>
  <li>Am I correct in using a button_tag?</li>
  <li>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.</li>
  <li>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.</li>
  </ol>
  
  <p>As can be seen in the other question I got something working but am now stuck and it's becoming quite desperate.</p>
  
  <p>I would be grateful for any help and sample code.</p>
  
  <p>Thanks a ton!</p>