rails form ajax,Submitting a Rails form with jQuery and Ajax

EDIT: Figured it out so asking a related question.

here's my Javascript

jQuery.ajaxSetup({

'beforeSend': function(xhr) {

xhr.setRequestHeader("Accept", "text/javascript")

}

})

jQuery.fn.submitWithAjax = function() {

this.submit(function() {

$.post(this.action, $(this).serialize(), null, "script");

return false;

})

return this;

};

$('.error').hide();

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

// validate and process form here

$('.error').hide();

var name = $("input#business_name").val();

if (name == "" || name == "Required Field") {

$('#namelabel').show()

$("#business_name").focus();

return false;

}

var address = $("#business_address").val();

if (address == "" || address == "Required Field") {

$('#addresslabel').show();

$("#business_address").focus();

return false;

}

var city = $("#business_city").val();

if (city == "" || city == "Required Field") {

$('#citylabel').show();

$('#business_city').focus();

return false;

}

var state = $("#business_state").val();

if (state == "" || state == "Required Field") {

$('#statelabel').show();

$("#business_state").focus();

return false;

}

var zip = $("#business_zip").val();

if (zip == "" || zip == "Required Field") {

$('#ziplabel').show();

$("#business_zip").focus();

return false;

}

var phone = $("#business_phone").val();

if (phone == "" || phone == "Required Field") {

$('#phonelabel').show();

$("#business_phone").focus();

return false;

}

var category = $("#business_business_category_id").val();

if (category == " - Choose one - ") {

$('#categorylabel').show();

$("#business_business_category_id").focus();

return false;

}

$.ajax ({

type: "POST",

data: form.serialize()

});

return false;

})

the .ajax code fires my create.js.erb file which contains

$("#new_business").submitWithAjax();

$("#new_business")[0].reset();

$("#new_business").hide();

Here's the table below the form.

'Approve'), :id =>@business.id, :action => 'approve') %>

Edit

'Remove'), @business, :confirm => 'Are you sure?', :method => :delete) %>

Now my only problem is that the table below my form isn't getting refreshed dynamically. It adds the data to the database fine and all the validation works great. But I have to refresh the page. I tried adding something like

$("#unapproved").append(" 'business')) %>");

but that just breaks it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值