.js.erb VS .js

What is the advantage to putting you javascript for your rails app into a .js.erb file instead of just throwing it in your application.js file? I have a create button for businesses so should I put the code into a create.js.erb file or put it into my application.js using:

$("#business_submit").click(function() {}

All that aside is that I have my create button

$("#business_submit").click(function() {
    // validate and process form here

    var name = $("input#business_name").val();
    if (name == ""  || name == "Required Field") {
        return false;
    var address = $("#business_address").val();
    if (address == ""  || address == "Required Field") {
        return false;
    var city = $("#business_city").val();
    if (city == "" || city == "Required Field") {
        return false;
    var state = $("#business_state").val();
    if (state == ""  || state == "Required Field") {
        return false;
    var zip = $("#business_zip").val();
    if (zip == ""  || zip == "Required Field") {
        return false;
    var phone = $("#business_phone").val();
    if (phone == ""  || phone == "Required Field") {
        return false;

     var category = $("#business_business_category_id").val();
    if (category == " - Choose one - ") {
        return false;
   return false;

This works great in my .js file but when I click create it doesn't actually send the information they typed in the form to the database. So I tried copy pasting the code into the .js.erb file. Now the form is submitted to the database when I click create, but half the javascript isn't working. The $('.error').hide(); isn't hiding my errors and they are all showing up regardless. $("#new_business")[0].reset(); isn't reseting my form, and there is no validation checks when I click create. So I either have a fully functional form that doesn't submit or one that submits but isn't really functional. Which one should I try to get working?

I see a lot of code with $.ajax but I can't figure it out for the life of me. I tried for a bit and I started getting some forgery protection errors in rails which is a whole other problem.



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.