Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:02:09+00:00 2026-06-02T02:02:09+00:00

I have the following form in my edit.html.erb page <%= form_for(@device) do |f| %>

  • 0

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

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.

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 %>');
     });
})

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?

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.

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.

Thanks a lot!

Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:02:11+00:00Added an answer on June 2, 2026 at 2:02 am

    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');
         });
    })
    

    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.

    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().

    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.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form with the following HTML below: <form id=course_edit_form name=course_form action=/courses/save method=post>
I have the following form in my /app/views/password_resets/new.html.erb view <% form_tag password_resets_path do %>
I have the following form: HTML <form id="myForm" action="" method="post"> <div id="edit-box"> <INPUT type="submit"
In my Ruby on Rails code, I have the following edit.html.erb file for tasks:
Here's what I have in my users/edit.html/erb <%= form_for(resource, :as => resource_name, :url =>
I have the following form: @model Teesa.Models.SearchModel @using (Html.BeginForm(Index, Search, FormMethod.Get, new { id
I have the following form: echo $this->Form->input('Person.name'); echo $this->Form->input('Person.phone'); echo $this->Form->input('Person.email'); echo $this->Form->input('Message.0.plainmsg'); echo
I have the following form <form name=myForm id=myForm method=post enctype=multipart/form-data action=script.php> and this jQuery
i have the following form item { fieldLabel:'Username' ,id:username ,name:'username' ,allowBlank:false ,plugins:[Ext.ux.plugins.RemoteValidator] ,rvOptions: {
I have the following HTML and CSS: <button id=myBtn dojoType=dijit.form.Button>Testing</button> #myBtn { margin-left: 100px;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.