【问题标题】:How to Format to_json output to go well with update_attributes如何格式化 to_json 输出以配合 update_attributes
【发布时间】:2014-09-01 06:27:58
【问题描述】:

我正在开发 Angular JS + Rails 项目。我将 JSON 下面传递给由 rails to_json 创建的 angularJS

user_controller.rb

render :json => current_user.to_json(:include => [:projects,:courses,:awards]) 

用户控制器输出

{"_json"=>   
   [{"id"=>21,
    "name"=>"Demo Name",
    "email"=>"demo@demo.com",
    "mobile"=>"",
    "dob"=>nil,
    "gender"=>nil,
    "address"=>nil,
    "intro"=>nil,
    "created_at"=>"2014-09-01T05:29:38.000Z",
    "updated_at"=>"2014-09-01T05:29:38.000Z",
    "projects"=>[{"id":1,"name":"Existing Project"}],
    "courses"=>[],
    "awards"=>[]
   }] 
}

Assigning data to angular $scope.userData = response._json

在 Angular View 中,使用 HTML5 contenteditable(角度指令),我正在更改上述对象的值。

角度视图

<h3 ng-model="userData[0].name" contenteditable="true"></h3>

内容正在按预期变化。

{"_json"=>   
      [{"id"=>21,
        "name"=>"UPDATED NAME HERE",
        "email"=>"demo@demo.com",
        "mobile"=>"",
        "dob"=>nil,
        "gender"=>nil,
        "address"=>nil,
        "intro"=>nil,
        "created_at"=>"2014-09-01T05:29:38.000Z",
        "updated_at"=>"2014-09-01T05:29:38.000Z",
        "projects"=>[{"id":1, "name":"Existing Project"},{"name":"ADDED NEW PROJECT HERE"}],
        "courses"=>[{"name":"ADDED NEW COURSE HERE"}],
        "awards"=>[]
       }] 
    }

现在我需要将此对象发布到服务器并更新用户数据和所有关联数据,例如既有现有记录又有新记录的项目,我需要发布的正确方法和 JSON 格式是什么,以使这个过程变得简单可能。

【问题讨论】:

    标签: ruby-on-rails json angularjs activerecord ruby-on-rails-4


    【解决方案1】:

    用户角度 http 帖子。

    $http(
       method: 'POST'
       data: your_json_array_here
       url: your_url_to_controller_action
    
    ).success((data,status) -> 
    
    
    ).error((data,status) -> 
    
    
    )
    

    【讨论】:

      【解决方案2】:

      开始使用Angular Rails Resoures,另一个有用的参考是RestAngular

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多