简体   繁体   English

在多模型视图导轨中使用Has_and_belongs_to_many关联

[英]Using Has_and_belongs_to_many association in multi model view rails

I am new to rails and having trouble with views so excuse me if this is a simple question. 我是新手,对视图有麻烦,所以请问这是一个简单的问题。 I was wondering how add to a has_and_belongs_to_many association in the view. 我想知道如何添加到视图中的has_and_belongs_to_many关联。

Here is the situation I have a model with the following mongoid documents: 在这种情况下,我有一个模型,该模型包含以下蒙古文件:

class Project
  include Mongoid::Document
  field :name, type: String
  key :name
  field :numPeople, type: Integer
  has_and_belongs_to_many :people
end

and... 和...

class Person
  include Mongoid::Document
  field :name, type: String
  key :name
  field :numProjects, type: Integer, default: 0
  has_and_belongs_to_many :projects
end

What I want is a Person page with the ability to write the name of a project in a text field and if the project exists it will add him to the project and if it doesn't exist it will create a new project with the name he specified. 我想要的是一个Person页,它能够在文本字段中写一个项目的名称,如果该项目存在,它将把他添加到该项目中;如果不存在,它将创建一个名称为他的新项目。指定。

What I want is similar to this (in HAML): 我想要的与此类似(在HAML中):

= form_for Project.new do |f|
    %p
        = f.text_field :name
        = f.submit "Add Project"

but I do not want to create a new project each time because the project may already exist. 但我不想每次都创建一个新项目,因为该项目可能已经存在。

I looked up stuff on formtastic and nested_form but did not see a perfect example that matched my scenario. 我在formtastic和nested_form上查找内容,但没有找到与我的情况相符的完美示例。

Thanks 谢谢

I'm a noob, but i think i can help - not for the view part, but for the logic beneath. 我是菜鸟,但我想我可以提供帮助-不是针对视图,而是针对下面的逻辑。

It seems you will need an action in your PersonPagesController that will be called on form submission (admitting that your form sends a person_name and a project_name), that should look like : 看来您将需要在PersonPagesController中执行一个操作,该操作将在表单提交时被调用(承认您的表单发送了一个person_name和一个project_name),该操作应类似于:

def add_person_to_project
  @person = Person.find(params[:person_name])
  @project = Project.find_or_create_by_name(params[:project_name])
  @project.persons << @person # or @person.projects << @project
end

information on find_or_create can be found there http://api.rubyonrails.org/classes/ActiveRecord/Base.html (under "Dynamic Attribute-based Finders") 可以在http://api.rubyonrails.org/classes/ActiveRecord/Base.html (在“基于动态属性的查找器”下)找到有关find_or_create的信息。

you will obviously have to send the person's name with the form for this to work, which you should be able to do by adding a pre-filled hidden field when generating the empty form for a specific person (i don't know formstatic and nested_form though...). 您显然必须发送此人的姓名和表格才能使用,这可以通过在为特定人生成空表格时添加一个预填充的隐藏字段来实现(我不知道formstatic和nested_form虽然...)。 You may also want to secure this a bit ( check if a person is really found, and if that person does not already belong to the project before adding it...) 您可能还需要对此加以保护(在添加人员之前,请检查是否确实找到了该人员,以及该人员是否还不属于该项目...)

Also, i would generally not advise to use the name fields as keys for your persons and projects, but that's up to you. 另外,我通常不建议使用名称字段作为您的人员和项目的键,但这取决于您。 I would advise, though, that you implement this another way, letting the user choose the project in a list or create a new one (what happens if your user makes a typo for example ?) 不过,我建议您以另一种方式实现此方法,让用户在列表中选择项目或创建一个新项目(例如,如果您的用户输入错字会发生什么情况?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM