Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在“编辑”而非“新建”中添加关系时出现问题_Ruby On Rails - Fatal编程技术网

Ruby on rails 在“编辑”而非“新建”中添加关系时出现问题

Ruby on rails 在“编辑”而非“新建”中添加关系时出现问题,ruby-on-rails,Ruby On Rails,我试图为一个“事件”构建一个表单,允许用户使用一些javascript动态添加注释(文本字段)。因此,当他们单击表单中的“添加注释”按钮时,会弹出一个文本字段,他们可以添加注释。如果他们再次单击,将显示另一个字段。到目前为止,这在创建新事件时效果很好,但当我编辑事件并添加新字段时,它不会拾取事件注释和用户之间的关系 例如,以下是我创建新事件时看到的内容 INSERT INTO "incident_notes" ("created_at", "updated_at", "user_id", "n

我试图为一个“事件”构建一个表单,允许用户使用一些javascript动态添加注释(文本字段)。因此,当他们单击表单中的“添加注释”按钮时,会弹出一个文本字段,他们可以添加注释。如果他们再次单击,将显示另一个字段。到目前为止,这在创建新事件时效果很好,但当我编辑事件并添加新字段时,它不会拾取事件注释和用户之间的关系

例如,以下是我创建新事件时看到的内容

 INSERT INTO "incident_notes" ("created_at", "updated_at", "user_id", "note", "incident_id") VALUES('2010-07-02 14:07:42', '2010-07-02 14:07:42', 2, 'A Note', 8)
如您所见,user_id字段分配了一个编号。但在编辑过程中,当我添加另一个注释时,会发生以下情况:

INSERT INTO "incident_notes" ("created_at", "updated_at", "user_id", "note", "incident_id") VALUES('2010-07-02 14:09:11', '2010-07-02 14:09:11', NULL, 'Another note', 8)
用户id为空。我不确定我做了什么。控制器中“编辑”和“新建”的代码非常相似

def create
  @incident = @customer.incidents.build(params[:incident])
  @incident.capc_id = generate_capc_id
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.save #etc
end

def update
  @incident = @customer.incidents.find(params[:id])
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.update_attributes(params[:incident])
    #etc
end
我有以下模型和关系(仅显示相关部分):

也许有更好的方法可以做到这一点,但正如您在“创建”方法中所看到的,我必须手动将事件注释用户字段设置为当前用户。这很好,但在update方法中似乎不起作用


任何想法、建议和帮助都将受到欢迎!我现在很困。:)

我建议您没有直接属于用户的事件注释。换句话说,一个用户有许多事件,一个事件有许多事件注释

class Incident < ActiveRecord::Base
  has_many                :incident_notes
  belongs_to              :user
end

class IncidentNote < ActiveRecord::Base
  belongs_to :incident
end

class User < ActiveRecord::Base
  has_many :incidents
  has_many :incident_notes, :through => :incident
end
类事件:事件
结束

然后通过用户的事件模型获取用户的事件注释

问题在于,我希望用户能够为其他用户拥有的事件创建注释。如果我理解正确的话,这是不允许的,对吗?不,我相信如果事件的所有权属于原始用户,这仍然有效。关于用户B是否“有权”为用户A更新或创建注释的问题是一个特定于应用程序的概念,将由应用程序而不是通过数据库模式强制实施。只要您允许控制器操作允许“当前用户”查找属于其他用户的事件,您就可以了
def create
  @incident = @customer.incidents.build(params[:incident])
  @incident.capc_id = generate_capc_id
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.save #etc
end

def update
  @incident = @customer.incidents.find(params[:id])
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.update_attributes(params[:incident])
    #etc
end
class Incident < ActiveRecord::Base
  has_many                :incident_notes
  belongs_to              :user
end

class IncidentNote < ActiveRecord::Base
  belongs_to :incident
end

class User < ActiveRecord::Base
  has_many :incidents
  has_many :incident_notes, :through => :incident
end