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 9163429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:26:44+00:00 2026-06-17T14:26:44+00:00

I’m following Railcasts 197 in order to make a nested form to my application.

  • 0

I’m following Railcasts 197 in order to make a nested form to my application. What I’m trying to do is displaying a “remove” link which hides a row from the form using jQuery.

enter image description here

If I click in remove, nothing happens, but if I hit Submit and there are errors on page, the row is removed!

enter image description here

I’ve been trying to fix for days but I just can’t see if something is missing or I need to add something, any help would be appreciated.

Code:

model/album_review_proposal.rb

class AlbumReviewProposal < ActiveRecord::Base
  attr_accessible :description, :user_id, :album_attributes
  validates :description, :presence => true

  has_one :album, :dependent => :destroy
  accepts_nested_attributes_for :album , :allow_destroy => true
end

model/album.rb

class Album < ActiveRecord::Base
  attr_accessible :album_name, :artist,  :year, :tracks_attributes

  validates :album_name, :artist, :presence => true
  validates :year, :numericality => { :only_integer => true }

  belongs_to :album_review_proposal
  has_many :tracks
  accepts_nested_attributes_for :tracks, :allow_destroy => true
end

model/track.rb

class Track < ActiveRecord::Base
  attr_accessible :track_name, :track_number

  validates :track_name, :track_number,  :presence => true

  belongs_to :album
end

views/album_review_proposals/_form.html.erb

<%= form_for(@album_review_proposal) do |f| %>
  <% if @album_review_proposal.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@album_review_proposal.errors.count, "error") %> prohibited this album_review_proposal from being saved:</h2>

      <ul>
      <% @album_review_proposal.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <%= f.fields_for :album do |album| %>
        <div class="field">
          <%= album.label :album_name %><br />
          <%= album.text_field :album_name %>
        </div>
        <div class="field">
          <%= album.label :artist %><br />
          <%= album.text_field :artist %>
        </div>
        <div class="field">
          <%= album.label :year %><br />
          <%= album.text_field :year %>
        </div>

        <div class="field">
          <%= f.label :description %><br />
          <%= f.text_area :description %>
        </div>

        <legend>Tracks</legend>
        <table>
            <th>#</th>
            <th>Name</th>
            <th></th>

            <%= album.fields_for :tracks do |track|  %>
            <tr>
            <div  class="fields">
                <td><%= track.text_field :track_number, :size => 2 %></td>
                <td><%= track.text_field :track_name, :size => 50 %> </td>

              <td><%= track.hidden_field :_destroy %></td>
                <td><%= link_to_remove_fields "remove", track   %></td>
            </div>
            </tr>
            <% end %>
        </table>

    <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

controllers/album_review_proposals.controller.rb (Only modified method from scaffold)

  def new
    @album_review_proposal = AlbumReviewProposal.new

    @album = @album_review_proposal.build_album

    4.times { @album.tracks.build }

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @album_review_proposal  }
    end
  end

application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

function remove_fields(link) {
    $(link).prev("input[type=hidden]").val("1");
    $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
    var new_id = new Date().getTime();
    var regexp = new RegExp("new_" + association, "g")
    $(link).parent().before(content.replace(regexp, new_id));
}

application_helper.rb

module ApplicationHelper
  def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
  end


end

HTML part of the HTML I want to remove

 <legend>Tracks</legend>
        <table>
          <thead>
          <tr>
            <th>#</th>
            <th>Name</th>
            <th></th>
          </tr>
          </thead>
          <tbody>

            <div class="fields">
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_0_track_number" name="album_review_proposal[album_attributes][tracks_attributes][0][track_number]" size="2" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_0_track_name" name="album_review_proposal[album_attributes][tracks_attributes][0][track_name]" size="50" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_0__destroy" name="album_review_proposal[album_attributes][tracks_attributes][0][_destroy]" type="hidden" value="false" /><a href="#" onclick="remove_fields(this); return false;">remove</a></td>
            </tr>

            <div class="fields">
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_1_track_number" name="album_review_proposal[album_attributes][tracks_attributes][1][track_number]" size="2" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_1_track_name" name="album_review_proposal[album_attributes][tracks_attributes][1][track_name]" size="50" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_1__destroy" name="album_review_proposal[album_attributes][tracks_attributes][1][_destroy]" type="hidden" value="false" /><a href="#" onclick="remove_fields(this); return false;">remove</a></td>
            </tr>

            <div class="fields">
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_2_track_number" name="album_review_proposal[album_attributes][tracks_attributes][2][track_number]" size="2" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_2_track_name" name="album_review_proposal[album_attributes][tracks_attributes][2][track_name]" size="50" type="text" /></td>
              <td><input id="album_review_proposal_album_attributes_tracks_attributes_2__destroy" name="album_review_proposal[album_attributes][tracks_attributes][2][_destroy]" type="hidden" value="false" /><a href="#" onclick="remove_fields(this); return false;">remove</a></td>
            </tr>

            <!-- repeat 3 times... -->
            </tr>
          </tbody>
        </table>
  • 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-17T14:26:45+00:00Added an answer on June 17, 2026 at 2:26 pm

    In your view, at views/album_review_proposals/_form.html.erb, instead of adding the class fields to a div inside each tr of a track, add it directly to the tr. The problem is that the div doesn’t behave as expected when added inside a table row.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.