问题描述
我目前有两个型号骨干:用户和项目。我想有一个包含的形式,使新项目的创建这个项目骨干观点,目前现有用户(项从数据库用户表)的关联。当用户完成此表格,并点击保存按钮,新的项目应该被保存到数据库中(在项目表)和保存的项目和相关用户之间的关系应该被保存到一个关系表(projects_users表,含相应的项目ID和用户ID为每个关系)。目前,我可以保存该项目的信息,但使用骨干关系不能得到任何数据到projects_users表。
I currently have two Backbone models: user and project. I would like to have a Backbone view containing a form that enables the creation of a new project, and the association of currently existing users (entries from the database users table) with this project. When a user completes this form and clicks the save button, the new project should be saved into the database (in the projects table) and the relationship between the saved project and the related users should be saved into a relationship table (projects_users table, containing the corresponding project id and the user id for each relationship). Currently, I can save the project information, but could not get any data into the projects_users table using Backbone-relational.
你认为什么是最好的方式来实现上述功能是什么?这将是巨大的,如果你能指出我的具体code,我可以作为一个模板使用。
What do you think the best approach to achieve the above functionality is? It would be great if you could point me to specific code that I could use as a template.
感谢您,
亚历山德拉
Thank you,Alexandra
推荐答案
在一些令人沮丧的试验和错误的时间,我终于把我的code工作!它不漂亮,但它是功能性的,现在我可以开始考虑改进它。我们希望,其他一些人会发现此信息有用...
After some frustrating trial-and-error period, I finally managed to get my code working! It's not beautiful, but it is functional, and now I can start thinking about improving it. Hopefully, some other people will find this information useful ...
一件事,让我在正确的轨道上是理解,需要什么来改变不仅是在骨干视图code(一个新项目的形式),也可在相应的轨道模型
The one thing that put me on the right track was the understanding that what needed to be changed was not only in the backbone view code (the one with the new projects form), but also in the corresponding rails models.
有关导轨部分(滑轨3.2.2),我确信,以下模型文件有必要的信息:
For the rails part (Rails 3.2.2), I made sure that the following model files had the necessary information:
project.rb
class Project < ActiveRecord::Base
has_and_belongs_to_many :users
#attr_accessible :name, :description, :users_attributes
#has_many :projects_users, foreign_key: "project_id", dependent: :destroy
#has_many :users, through :projects_users
#accepts_nested_attributes_for :users
end
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :projects
end
projects_users.rb
class ProjectsUsers < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
我在has_and_belongs_to_many不设置在铁轨许多一对多关系的最佳途径很多不同的地方已经阅读。不过,我无法使用的has_many定义相同的功能 - 在project.rb注释的部分是这个我试过不同的方法方式。该文件user.rb有一些相应的code,这是我的简单删除。
I have read in many different places that has_and_belongs_to_many is not the best way to set many-to-many relationships in rails. Nevertheless, I could not get the same functionality using the has_many definition - the commented part in project.rb is the way I tried this different approach. The file user.rb had some corresponding code, which I removed for simplicity.
现在,我需要得到的骨干表单视图做的是发送POST请求以JSON对象导轨projects_controller.rb可以识别。起初,我尝试没有成功的几个POST请求(没有错误引导我)。但后来,我记得有previously实施了,用户可以添加到一个特定的团队团队形式(HABTM复选框 - 存在的)。看着这个例子后,我意识到从我的POST请求需要的是什么。这是我想在轨服务器日志文件中看到:
Now, what I needed to get done in the backbone form view was to send a POST request with a JSON object that the rails projects_controller.rb could recognize. Initially, I tried several POST requests without success (and no errors to guide me). But then, I remembered to have previously implemented a form for teams where users could be added to a particular team (HABTM Checkboxes - there is a railscast for this functionality). After looking at this example, I realized what was needed from my POST request. This is what I wanted to see in the rails server log file:
Started POST "/projects" for 127.0.0.1 at 2012-06-27 00:35:22 +0000
Processing by ProjectsController#create as JSON
Parameters: {"project"=>{"description"=>"with some description", "user_ids"=>["101", "1", "99"], "name"=>"some new project"}}
相关骨干文件来达到上述要求:
Backbone relevant files to achieve the above request:
project.js
App.Models.Project = Backbone.Model.extend({
urlRoot: '/projects',
// Default attributes for the project.
defaults: {
description: "",
user_ids: []
},
/* getters */
});
user.js的
App.Models.User = Backbone.Model.extend({
/* getters */
});
form.js
App.Views.Projects.Common.Form = Backbone.View.extend({
...
events: {
"submit #new_project_form" : "formSubmit"
},
formSubmit: function(event) {
this.submitted($(event.target));
return false;
},
submitted: function(formElement) {
var newData = this.serializeFormData(formElement);
this.model = new App.Models.Project({
name : newData.name,
description : newData.description
});
this.saveFormData(newData);
return false;
},
serializeFormData: function(formElement) {
var fields = formElement.serializeArray();
var serializedData = {};
$.each(fields, function(index, field) {
serializedData[field.name] = field.value;
});
return serializedData;
},
// THE IMPORTANT PART FOR THE POST REQUEST
saveFormData: function(newData) {
// preserve reference to view for callbacks
var self = this;
var project = this.model;
project.set({
// a list of user ids associated with a project
"user_ids" : this.view_variables.user_ids
});
var project_object = ({
"project" : _.clone(project.attributes)
});
$.ajax({
type: 'POST',
url: '/projects',
data: project_object,
dataType: "json",
success: function() {
self.$el.hide();
self.addNewModelToCollection();
}
});
},
...
});
在code是那种冗长,包括一些code是具体到我的项目。仍然,相关部分是在saveFormData功能,在使用了jQuery AJAX功能
The code is kind of verbose, and includes some code that is specific to my project. Still, the relevant part is in the saveFormData function, where the jQuery ajax function is used.
如果您有任何建议,无论是对铁轨或主干部分,请让我知道。我会很乐意学习如何提高这一解决方案。
In case you have any suggestions, either for the rails or for the Backbone part, please let me know. I will be happy to learn how to improve this solution.
这篇关于从骨干工程的条目保存到数据库的关系(很多到多)表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!