Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3 Rails 3阵列的问题_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 Rails 3阵列的问题

Ruby on rails 3 Rails 3阵列的问题,ruby-on-rails-3,Ruby On Rails 3,在我的Rails 3应用程序中,每个用户的配置文件都有一个我想添加到的数组。这些项目是唯一的,但会添加到两个类别之一的每个配置文件中。但是,我遇到的问题是通过其ID将profile_项链接到该项 profile.rb型号: class Profile < ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user has_many :profile_todos has_many :todo

在我的Rails 3应用程序中,每个用户的配置文件都有一个我想添加到的数组。这些项目是唯一的,但会添加到两个类别之一的每个配置文件中。但是,我遇到的问题是通过其ID将profile_项链接到该项

profile.rb型号:

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user

  has_many :profile_todos
  has_many :todos, :through => :profile_todos

  def add_todo_inside_item(item)
    self.profile_todos.build :category => 'inside'
  end

  def add_todo_outside_item(item)
    self.profile_todos.build :category => 'outside'
  end
end
class Todo < ActiveRecord::Base
  belongs_to :profile
end
class ProfileTodo < ActiveRecord::Base
  belongs_to :profile
  belongs_to :todo
end
>> Todo.new
=> #<Todo id: nil, name: nil, created_at: nil, updated_at: nil>
>> ProfileTodo.new
=> #<ProfileTodo id: nil, category: nil, profile_id: nil, created_at: nil, updated_at: nil>
<div id="todosheader">
  <p><%= @profile.first_name %>&nbsp;has&nbsp;<%= pluralize(@profile.profile_todos.count, 'todo') %>.</p>
</div>
<div id="todo-list">
  <div class="todos_inside">
    <h4>Inside&nbsp;(<%= @profile.profile_todos(:category => "inside").count %>)</h4>
    <p><%= @profile.profile_todos(:category => "outside") %></p>
  </div>
  <div class="todos_outside">
    <h4>Outside&nbsp;(<%= @profile.profile_todos(:category => "outside").count %>)</h4>
    <p><%= @profile.profile_todos(:category => "outside") %></p>
  </div>
</div>
ProfileTodo.new
提供以下内容:

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user

  has_many :profile_todos
  has_many :todos, :through => :profile_todos

  def add_todo_inside_item(item)
    self.profile_todos.build :category => 'inside'
  end

  def add_todo_outside_item(item)
    self.profile_todos.build :category => 'outside'
  end
end
class Todo < ActiveRecord::Base
  belongs_to :profile
end
class ProfileTodo < ActiveRecord::Base
  belongs_to :profile
  belongs_to :todo
end
>> Todo.new
=> #<Todo id: nil, name: nil, created_at: nil, updated_at: nil>
>> ProfileTodo.new
=> #<ProfileTodo id: nil, category: nil, profile_id: nil, created_at: nil, updated_at: nil>
<div id="todosheader">
  <p><%= @profile.first_name %>&nbsp;has&nbsp;<%= pluralize(@profile.profile_todos.count, 'todo') %>.</p>
</div>
<div id="todo-list">
  <div class="todos_inside">
    <h4>Inside&nbsp;(<%= @profile.profile_todos(:category => "inside").count %>)</h4>
    <p><%= @profile.profile_todos(:category => "outside") %></p>
  </div>
  <div class="todos_outside">
    <h4>Outside&nbsp;(<%= @profile.profile_todos(:category => "outside").count %>)</h4>
    <p><%= @profile.profile_todos(:category => "outside") %></p>
  </div>
</div>
如何将
todo
添加到配置文件中,其中
todo
:name
与给定的
:category
一起添加到配置文件中?现在调用
@profile.profile\u todos
返回一个空数组


假设我有一个“Test”的待办事项,ID为“1”。如果我尝试使用
@profile将
Todo
添加到我的
配置文件\u Todo
数组中。在项目(“Test”)中添加Todo(“Test”)
,我似乎无法将“Test”
Todo
附加到我的
配置文件\u Todo(:category=>“inside”)
我不确定您想在这里做什么

TODO应该是复数形式。。你应该救父母

class Profile < ActiveRecord::Base
    has_many :todos

    def new_inside_todo(params)  # takes a hash of all attributes, like new()
      params.merge!( {:category => "inside"} )
      self.todos.build( params )
      self.save(:validate => false)
    end

    def new_outside_todo(params)
      params.merge!( {:category => "outside"} )
      self.todos.build( params )
      self.save(:validate => false)
    end
end

class Todo < ActiveRecord::Base
   belongs_to :profile

   # has an attribute called category

   # if Todos belong to more than one class, make the relationship polymorphic
   #     belongs_to :parent , :polymorphic => true
end
类配置文件“inside”})
self.todos.build(参数)
save(:validate=>false)
结束
def new_外部_todo(参数)
合并!({:类别=>“外部”})
self.todos.build(参数)
save(:validate=>false)
结束
结束
类Todotrue
结束

从您的示例中,不清楚您要如何称呼它。。考虑到上面的代码,调用Todo.new或ProfileGoal.new并期望除空实例之外的任何内容都是没有意义的。我要做的是让所有用户都可以使用
Todo
来添加到他们的
配置文件\u Todo
。但我想按类别将TODO添加到
profile\u TODO
,可以是“内部”也可以是“外部”。所以我想取一个
todo
的名称(字符串值),给它一个类别,然后将它添加到
profile\u todo
。这样,我就可以将概要文件TODO分为
profile\u TODO(:category=>“内部”)
profile\u TODO(:category=>“外部”)
。希望这是有意义的。我只是在我的问题中添加了一个更新。我希望这能提供更多的见解。谢谢你的帮助!