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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:13+00:00 2026-05-25T09:53:13+00:00

I have this weird failing coffeescript, from this demo . The demo code works

  • 0

I have this weird failing coffeescript, from this demo. The demo code works fine on my local machine. And I’ve blatantly copied it, wholesale, into my own rails 3.1 app. There are no useful error messages but the buttons are acting as if they’re buttons which don’t know they have a script attached to them – they’re refreshing the page.

app/assets/application.js contains the compiled coffee script.
The compiled html is identical between the two apps.
Other javascripts are working.
I’ve deleted all css and javascript except this one, and it’s not working.
The coffeescript is fine as it’s working on the demo app on my machine.

Am I missing something blindingly obvious about how you use coffee script in the asset pipeline?

Code:

application.rb:

   # Enable the asset pipeline
    config.assets.enabled = true

Search.js.coffee:

class @Search
  constructor: (@templates = {}) ->

  remove_fields: (button) ->
    $(button).closest('.fields').remove()

  add_fields: (button, type, content) ->
    new_id = new Date().getTime()
    regexp = new RegExp('new_' + type, 'g')
    $(button).before(content.replace(regexp, new_id))

  nest_fields: (button, type) ->
    new_id = new Date().getTime()
    id_regexp = new RegExp('new_' + type, 'g')
    template = @templates[type]
    object_name = $(button).closest('.fields').attr('data-object-name')
    sanitized_object_name = object_name.replace(/\]\[|[^-a-zA-Z0-9:.]/g, '_').replace(/_$/, '')
    template = template.replace(/new_object_name\[/g, object_name + "[")
    template = template.replace(/new_object_name_/, sanitized_object_name + '_')
    $(button).before(template.replace(id_regexp, new_id))

Application helper:

 def setup_search_form(builder)
      fields = builder.grouping_fields builder.object.new_grouping, object_name: 'new_object_name', child_index: "new_grouping" do |f|
        render('grouping_fields', f: f)
      end
      content_for :document_ready, %Q{
        var search = new Search({grouping: "#{escape_javascript(fields)}"});
        $('button.add_fields').live('click', function() {
          search.add_fields(this, $(this).data('fieldType'), $(this).data('content'));
          return false;
        });
        $('button.remove_fields').live('click', function() {
          search.remove_fields(this);
          return false;
        });
        $('button.nest_fields').live('click', function() {
          search.nest_fields(this, $(this).data('fieldType'));
          return false;
        });
      }.html_safe
    end

    def button_to_remove_fields(name, f)
      content_tag :button, name, class: 'remove_fields'
    end

    def button_to_add_fields(name, f, type)
      new_object = f.object.send "build_#{type}"
      fields = f.send("#{type}_fields", new_object, child_index: "new_#{type}") do |builder|
        render(type.to_s + "_fields", f: builder)
      end
      content_tag :button, name, :class => 'add_fields', 'data-field-type' => type, 'data-content' => "#{fields}"
    end

    def button_to_nest_fields(name, type)
      content_tag :button, name, :class => 'nest_fields', 'data-field-type' => type
    end

advanced_search.html.erb

<h1>Advanced ISBN Search</h1>
<%= link_to 'Simple mode', isbns_path %>

<%= search_form_for @search, url: advanced_search_isbns_path, html: {method: :post} do |f| %>
  <% setup_search_form f %>

  <fieldset>
    <legend>Sorting</legend>
    <%= f.sort_fields do |s| %>
      <%= render 'sort_fields', f: s %>
    <% end %>
    <%= button_to_add_fields "Add Sort", f, :sort %>
  </fieldset>

  <fieldset>
    <legend>Condition Groups</legend>
    <%= f.grouping_fields do |g| %>
      <%= render 'grouping_fields', f: g %>
    <% end %>
    <%= button_to_add_fields "Add Condition Group", f, :grouping %>
  </fieldset>

  <%= label_tag :distinct, 'Return distinct records?' %>
  <%= check_box_tag :distinct, '1', params[:distinct].to_i == 1 %>
  <%= f.submit %>
<% end %>

<%= render 'results' %>

Example partial: _grouping_fields.html.erb

<fieldset class="fields" data-object-name="<%= f.object_name %>">
  <legend>Match <%= f.combinator_select %> conditions <%= button_to_remove_fields "remove", f %></legend>
  <%= f.condition_fields do |c| %>
    <%= render 'condition_fields', f: c %>
  <% end %>
  <%= button_to_add_fields "Add Condition", f, :condition %>

  <%= f.grouping_fields do |g| %>
    <%= render 'grouping_fields', f: g %>
  <% end %>
  <%= button_to_nest_fields "Add Condition Group", :grouping %>
</fieldset>

Update / solution.

Firebug pointed out that there’s a conflict with some other js, in the Cocoon gem. Here’s its code:

 $('.add_fields').live('click', function() {
    var assoc   = $(this).attr('data-association');
    var assocs  = $(this).attr('data-associations');
    var content = $(this).attr('data-template');
    var insertionPosition = $(this).attr('data-association-insertion-position');
    var insertionNode = $(this).attr('data-association-insertion-node');
    var insertionCallback = $(this).data('insertion-callback');
    var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');
    var regexp_underscord = new RegExp('_new_' + assoc + '_', 'g');
    var new_id  = new Date().getTime();
    var newcontent_braced = '[' + new_id + ']';
    var newcontent_underscord = '_' + new_id + '_';
    var new_content = content.replace(regexp_braced, '[' + new_id + ']');
    if (new_content == content) {
        regexp_braced = new RegExp('\\[new_' + assocs + '\\]', 'g');
        regexp_underscord = new RegExp('_new_' + assocs + '_', 'g');
        new_content = content.replace(regexp_braced, '[' + new_id + ']');
    }
    new_content = new_content.replace(regexp_underscord, newcontent_underscord);

    if (insertionNode) {
      insertionNode = $(insertionNode);
    }
    else {
      insertionNode = $(this).parent();
    }

    var contentNode = $(new_content);

    if (insertionPosition == 'after'){
      insertionNode.after(contentNode);
    } else {
      insertionNode.before(contentNode); 
    }

    if(insertionCallback){
      insertionCallback.call(contentNode);
    }

    return false;
  });

The conflicting line is

var new_content = content.replace(regexp_braced, '[' + new_id + ']');

Renaming add_fields to add_s_fields in application_helper and the coffeescript sorted it out.

  • 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-25T09:53:13+00:00Added an answer on May 25, 2026 at 9:53 am

    You inject some JS code with

    content_for :document_ready %Q{ ... }
    

    but I don’t see the requisite

    <%= content_for :document_ready %>
    

    in your ERB markup. Can you verify that the JS code is on the page? If so, where is it—are you sure it’s being run after class @Search is defined?

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

Sidebar

Related Questions

I have this weird problem with setting up cookies with PHP. Everything worked fine
This is very weird. I have the following code: Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact(2000,
I have this weird code on site. It gets all records (order by pozycja),
Good day, I have this weird problem: This following statement works Query q =
I have this exported file of some weird (standard for this industry!) format, which
This is a weird problem I have started having recently. My team is developing
This could be weird, Have you ever come across a blog which you wanted
This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
Sounds like a weird question, but say I have something like this: $.post( /myajax.php,

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.