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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:11+00:00 2026-06-04T04:15:11+00:00

I have developed a back-end in Rails 3.2 that allows admins to perform CRUD

  • 0

I have developed a back-end in Rails 3.2 that allows admins to perform CRUD operations using ajax and jQuery. I have done this with several controller/views using js.coffee files to handle the ajax. an example of my file structure for my views is as follows.

views
  users
    index.html.erb
    _form.html.erb
    _edit_form.html.erb
    _user.html.erb
    create.js.coffee
    edit.js.coffee        
    update.js.coffee
    destroy.js.coffee
  customers
    index.html.erb
    _form.html.erb
    _edit_form.html.erb
    _customer.html.erb
    create.js.coffee
    edit.js.coffee        
    update.js.coffee
    destroy.js.coffee
   email_blasts
     index.html.erb
     _form.html.erb
     _edit_form.html.erb
     _blast.html.erb
     create.js.coffee
     edit.js.coffee        
     update.js.coffee
     destroy.js.coffee
  .
  .
  .
  .

An example create.js.coffee for both the User views and Customer views is below. Notice the instance variables and div names that follow an exact pattern. All coffeescript below follows the exact structure in all the views that I have listed above. All create.js.coffee files listed above are virtually identical. My question is what’s the best way to reduce the duplication? Would this be something that I should create a plugin for? Should I put it in a helper…etc. I’m not sure how to handle this in rails as I am a former php programmer 🙁 Anyones help would be appreciated!

create.js.coffee for the User views:

$('#errors').empty()
$('#errors').show()
<% if @user.errors.any? %>
  $('<%= escape_javascript(render :partial => "errors", :locals => {:target => @user })%>')
    .appendTo('#errors')
<% else %>
  $('<%= escape_javascript(render(:partial => @user))%>')
    .appendTo('#user_table')
    .hide()
    .fadeIn(200)
$('#errors').hide()
$('#new_user')[0].reset()
$('#users_count').html '<%= users_count %>'
<% end %>
$('#error_close').click ->
  $('#errors').fadeOut() 

create.js.coffee for the Customer view

$('#errors').empty()
$('#errors').show()
<% if @customer.errors.any? %>
  $('<%= escape_javascript(render :partial => "errors", :locals => {:target => @customer })%>')
    .appendTo('#errors')
<% else %>
  $('<%= escape_javascript(render(:partial => @customer))%>')
    .appendTo('#customer_table')
    .hide()
    .fadeIn(200)
  $('#errors').hide()
  $('#new_customer')[0].reset()
  $('#customers_count').html '<%= customers_count %>'
<% end %>
$('#error_close').click ->
  $('#errors').fadeOut()
  • 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-04T04:15:12+00:00Added an answer on June 4, 2026 at 4:15 am

    You should structure your code and if you want to reuse it stop mixing javascript and erb. Make JavaScript classes which do the job and put the data in side a script tag in the page…

    Try to use some framework which helps you by giving structure like Backbone.js.

    This could be useful to you:

    • http://documentcloud.github.com/backbone/
    • http://backbonetutorials.com/
    • http://ricostacruz.com/backbone-patterns/

    Or the messy way:

    # Function 
    foo = (condition, firstBlock, secondBlock) ->
      $('#errors').empty()
      $('#errors').show()
      if condition
        firstBlock
          .appendTo('#errors')
      else
        secondBlock
          .appendTo('#customer_table')
          .hide()
          .fadeIn(200)
        $('#errors').hide()
        $('#new_customer')[0].reset()
        $('#customers_count').html '<%= customers_count %>'
      $('#error_close').click ->
        $('#errors').fadeOut()
    
    # Execution
    foo(<% @user.errors.any? %>, $('<%= stuff %>'), $('<%= moreStuff %>'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a EDM (phoneDB) that models a back-end MSSQL database. I've developed a
I have developed a Java server using Eclipse that accepts TCP socket connection from
I have a RESTful web-service application that I developed using the Netbeans IDE. The
I have developed a small application with SQL SERVER back end and I also
I have developed the following C code to mask data before sending back to
I have developed a web site that requires user registration and authentication for some
I have developed a website using Razor (Microsoft WebMatrix) and now I want to
I have developed an app that in testing has worked fine but when it
I have a small web application developed using Icefaces 1.8.2 and jsf 1.1 which
Our desktop application consists of a Mono/.NET 3.5 back end that communicates via USB

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.