.js.erb VS .js [英] .js.erb VS .js
问题描述
将您的JavaScript应用程序放入.js.erb文件而不是将其放入application.js文件中的优点是什么?我有一个创建企业按钮,所以我应该把代码放到一个create.js.erb文件中,或者把它放到我的application.js文件中:
$(#business_submit)。click(function(){}
全部除了我有我的创建按钮
$('。error')。hide();
$ (#business_submit)。click(function(){
//验证并处理表单
$ b $('。error')。hide();
var name = $(input#business_name)。val();
if(name ==|| name ==必填字段){
$('#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 ==必填字段){
$('#citylabel')。show();
$('#business_city')。focus();
返回false;
}
var state = $(#business_state)。val();
if(state ==|| state ==Required Field){
$('#statelabel')。show();
$(#business_state)。focus();
返回false;
}
var zip = $(#business_zip)。val();
if(zip ==|| zip ==Required Field){
$('#ziplabel')。show();
$(#business_zip)。focus();
返回false;
}
var phone = $(#business_phone)。val();
if(phone ==|| phone ==Required Field){
$('#phonelabel')。show();
$(#business_phone)。focus();
返回false;
}
var category = $(#business_business_category_id)。val();
if(category == - Choose one - ){
$('#categorylabel')。show();
$(#business_business_category_id)。focus();
返回false;
}
$(#new_business)[0] .reset();
$(#new_business)。toggle();
返回false;
});
这对我的.js文件非常有用,但是当我点击创建时它并不实际发送信息他们在表单中输入数据库。所以我试着将代码粘贴到.js.erb文件中。现在,当我单击创建时,表单被提交到数据库,但是一半的JavaScript不起作用。 $('。error')。hide();没有隐藏我的错误,他们都显示了不管。 $( #new_business)[0] .reset段();没有重置我的表单,并且当我点击创建时没有验证检查。所以我要么有一个没有提交的功能完整的表单,要么提交但没有实际功能的表单。我应该尝试哪一个工作?
我用$ .ajax看到很多代码,但是我无法想象出我的生活。我尝试了一下,并开始在rails中获得一些伪造保护错误,这是一个其他问题。 <$ c当操作完成时要执行javascript时,$ c> .js.erb 文件用于控制器操作,如create。例如,当你想通过ajax提交一个表单并且想要在所有事情完成时显示一个提示信息,你可以把
在您的application.js或*中点击$('#business_submit')。click(...)
.html.erb和你的create.js.erb可能如下所示:
alert('New object id:'+ <%= new_object.id%>);
通过这种方式,返回到浏览器的JavaScript可以首先由Erb呈现,从而允许您插入自定义字符串/变量/等,就像你可以在.html.erb模板中一样。
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
$('.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;
}
$("#new_business")[0].reset();
$("#new_business").toggle();
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.
.js.erb
files are for controller actions, such as create, when you want javascript to be executed when the action completes. For example, when you want to submit a form via ajax and want display an alert when everything is done, you would put the
$('#business_submit').click(...)
in your application.js or *.html.erb, and your create.js.erb could look like this:
alert('New object id: ' + <%= new_object.id %>);
This way, the javascript that is getting returned to the browser can be first rendered by Erb, allowing you to insert custom strings/variables/etc just like you can in .html.erb templates.
这篇关于.js.erb VS .js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!