Jquery 我该如何处理这个Padrino AJAX调用?

Jquery 我该如何处理这个Padrino AJAX调用?,jquery,sinatra,http-post,padrino,Jquery,Sinatra,Http Post,Padrino,我正在编写一个应用程序,主要是通过AJAX调用后端服务器的UI。页面加载很少 例如,当我创建一个trip时,我的JS只向Padrino发送一个JSON对象(通过POST),Padrino保存trip对象(通过ActiveRecord)并返回一个JSON响应 这似乎是可行的,但我不仅希望清理代码,而且希望清理提交的值 这是我的POST代码(trips controller) 目前,只有两个字段(标题和说明),但完成后将有大约4-8个字段。我不喜欢构建新的trip对象的方式 我尝试使用: @trip

我正在编写一个应用程序,主要是通过AJAX调用后端服务器的UI。页面加载很少

例如,当我创建一个trip时,我的JS只向Padrino发送一个JSON对象(通过POST),Padrino保存trip对象(通过ActiveRecord)并返回一个JSON响应

这似乎是可行的,但我不仅希望清理代码,而且希望清理提交的值

这是我的
POST
代码(
trips controller

目前,只有两个字段(标题和说明),但完成后将有大约4-8个字段。我不喜欢构建新的trip对象的方式

我尝试使用:

@trip = Trip.build(params[:trip])
但这并不奏效

以下是我发送帖子的JS代码:

// Save new trip
$("#new_trip_save_btn").click(function() {
    var self = this;
    var new_trip = get_new_trip();

    $.ajax({
        data: {trip:new_trip},
        dataType: "json",
        url: "/trips",
        type: "post", 
        success: function(res) {
            console.log(res)
        }
    });
});

......

var get_new_trip = function() {
    var self = this;
    var trip = {};
    trip.title = $("#new_trip_title").val();
    trip.description = $("#new_trip_description").val();
    trip.departure_date = $("#new_trip_departure").val();
    trip.return_date = $("#new_trip_return").val();

    return trip;
}
因此,我可以做些什么来清理代码(删除POST操作中的冗余),并确保在保存之前对文本进行了清理


谢谢

首先,您应该使用attr_-accessible和attr_-protected来保护您的模型不受批量指定的影响

然后我强烈建议使用“表单”,这样您的站点就可以在不启用javascript的情况下工作

因此,使用javascripts代码也可以更好

// Live watch events on form submit
$(document).on('submit', 'form[data-remote]', function(e){
  e.preventDefault();
  self = $(this);
  $.ajax({
    url: self.attr('action'),
    data: self.serializeArray(),
    type: self.attr('method'),
    dataType: 'json',
    success: function(res){ console.log(res) }
  })
});
表格如下:

/* Here the form, for dates I suggest to use a calendar */ 
/* like https://github.com/arshaw/fullcalendar */
- form_for @trip, url(:trip, :index), :'data-remote' => true do |f|
  == f.error_messages
  == f.text_field :title
  == f.text_area :description
  == f.text_field :departure_date
  == f.text_field :return_data
  == f.submit 'Send'
这里是控制器:

provides :json # if all your actions in your controller respond to #json it is more dry

get :index do
  @trip = Trip.new(params[:trip])

  if @trip.save
    render :success => true, :attributes => @trip.to_json
  else
    render :success => false, :errors => @trip.errors.full_messages
  end
end

首先,您应该使用attr_-accessible和attr_-protected来保护您的模型不受质量分配的影响

然后我强烈建议使用“表单”,这样您的站点就可以在不启用javascript的情况下工作

因此,使用javascripts代码也可以更好

// Live watch events on form submit
$(document).on('submit', 'form[data-remote]', function(e){
  e.preventDefault();
  self = $(this);
  $.ajax({
    url: self.attr('action'),
    data: self.serializeArray(),
    type: self.attr('method'),
    dataType: 'json',
    success: function(res){ console.log(res) }
  })
});
表格如下:

/* Here the form, for dates I suggest to use a calendar */ 
/* like https://github.com/arshaw/fullcalendar */
- form_for @trip, url(:trip, :index), :'data-remote' => true do |f|
  == f.error_messages
  == f.text_field :title
  == f.text_area :description
  == f.text_field :departure_date
  == f.text_field :return_data
  == f.submit 'Send'
这里是控制器:

provides :json # if all your actions in your controller respond to #json it is more dry

get :index do
  @trip = Trip.new(params[:trip])

  if @trip.save
    render :success => true, :attributes => @trip.to_json
  else
    render :success => false, :errors => @trip.errors.full_messages
  end
end

谢谢你的回复。然而,我的逻辑是使用nginx呈现静态页面,并简单地使用Padrino作为后端。我将在JS UI上花费大量时间,因此没有JS意味着没有访问权限。有些人不喜欢。但别这样,现在是2012年。哈哈。你对用主干网取代我的自定义JS ajax帖子有什么想法?这要看情况,主干网是个不错的选择,但如果应用程序逻辑不是那么复杂,最好从头开始写几行JS。我的代码“实时”查看所有具有“数据远程”属性的表单,并将其转换为ajax请求。这是一个“标准”的rails ujs/padrino ujs调用,它总是很有用的。关于主干网,这是非常正确的。我实际上是在nginx提供的一个HTML页面中呈现index/new/edit表单。然后,我根据您要做的事情隐藏每个部分。使页面加载看起来是瞬时的。:-)谢谢你的回复。然而,我的逻辑是使用nginx呈现静态页面,并简单地使用Padrino作为后端。我将在JS UI上花费大量时间,因此没有JS意味着没有访问权限。有些人不喜欢。但别这样,现在是2012年。哈哈。你对用主干网取代我的自定义JS ajax帖子有什么想法?这要看情况,主干网是个不错的选择,但如果应用程序逻辑不是那么复杂,最好从头开始写几行JS。我的代码“实时”查看所有具有“数据远程”属性的表单,并将其转换为ajax请求。这是一个“标准”的rails ujs/padrino ujs调用,它总是很有用的。关于主干网,这是非常正确的。我实际上是在nginx提供的一个HTML页面中呈现index/new/edit表单。然后,我根据您要做的事情隐藏每个部分。使页面加载看起来是瞬时的。:-)