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

  • Home
  • SEARCH
  • 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 3968936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:54:12+00:00 2026-05-20T03:54:12+00:00

I have a form that works, but I need to have some fields hidden

  • 0

I have a form that works, but I need to have some fields hidden unless the user clicks a link. Once they are exposed, everything is good. Here is the partial responsible for the form:

 <%= fields_for :city do |builder| %>
<p class="fields>
    <%= builder.label :name, "City" %>
    <%= builder.hidden_field :name %><br />
    <%= builder.label :state, "State" %>
    <%= builder.text_field :state %>
    <%= link_to_function "new city", "show_fields('city_name')" %>
</p>
<% end %>

Then in application.js

 function show_fields(link) {  
    $(link).previous("input[type=hidden]").show;
}  

When I try to run that and inspect it in chrome, I see the following error:

application.js4:Uncaught TypeError:Object# has no method ‘previous’

Now, I admit I am totally new to js and still pretty new to ruby and rails, but I am pretty sure I am doing exactly what Ryan Bates did in one of the railscasts. (I have tried the show_fields(this) format too with the same result).

What is the correct way to target my hidden_field with a link that exposes it? This seems so simple, but I can’t figure out why the previous method doesn’t work. Sorry if this is obvious, but I seriously can’t find the right place to start.

  • 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-05-20T03:54:12+00:00Added an answer on May 20, 2026 at 3:54 am

    From what I can tell, the show method (or whatever it is called in JS) is not available for the fields that are created by the hidden_field helper. I got the idea to add a function to hide an existing text field and then use the show method on it and that worked just fine.

    My solution then was to switch to jQuery and use:

    $(document).ready(function() {
     // hides the div as soon as the DOM is ready
      $('div.newcity').hide();
    });
    

    to hide the div before the page is displayed to the user. Then I can easily show that div using:

    function toggle_fields(link) {  
        $(link).toggle();
        $(link).prev().toggle();
    }
    

    in application.js and:

    <%= link_to_function "new city", "toggle_fields('div.newcity')" %>
    

    in the view. I had to use the jquery-rails gem to get all of this working if anyone happens to stumble on this later.

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

Sidebar

Related Questions

I currently have form that checks if a user has unsubmitted changes when they
I have an RMA form that I've been able to add fields dynamically, but
I have a page that has a form once the user submits the form
I have a continuous form that works when I manually open it. However when
I have a form that calls on two separate models. My validation works correctly
I have an C# form application that use an access database. This application works
I have a form that and after the user fills in this form, I
I have a form built with some custom validators and they work like a
I have a form that performs calculations live as the user enters data. However,
I have a small form and I need jquery to clear the hidden form

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.