Ruby on rails 通过具有多个ID的关联工作';s(轨道4.2.3)

Ruby on rails 通过具有多个ID的关联工作';s(轨道4.2.3),ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在完成我的第一个Rails项目。我正在创建一个应用程序,其中用户登录并创建一个板,该板特定于用户的ID。在该板中,用户可以创建列表。每个列表应与该板的ID绑定。我遇到的问题是创建一个列表。它不能保存。我看到的具体错误是找不到关联:模型列表中的成员身份我可以看到列表映射到板的ID时出现问题。当我查看Rails服务器的输出时,我看到显示板(应该显示该板的所有列表)没有用户ID或板ID 为用户显示电路板 Started GET "/boards" for ::1 at 2016-10-12 14:

我正在完成我的第一个Rails项目。我正在创建一个应用程序,其中用户登录并创建一个板,该板特定于用户的ID。在该板中,用户可以创建列表。每个列表应与该板的ID绑定。我遇到的问题是创建一个列表。它不能保存。我看到的具体错误是
找不到关联:模型列表中的成员身份
我可以看到列表映射到板的ID时出现问题。当我查看Rails服务器的输出时,我看到显示板(应该显示该板的所有列表)没有用户ID或板ID

为用户显示电路板

Started GET "/boards" for ::1 at 2016-10-12 14:26:33 -0400
Processing by BoardsController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Board Load (0.1ms)  SELECT "boards".* FROM "boards" WHERE "boards"."user_id" = ?  [["user_id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered boards/index.html.erb within layouts/application (3.4ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 168ms (Views: 166.1ms | ActiveRecord: 0.2ms)
Started GET "/lists.3" for ::1 at 2016-10-12 14:28:26 -0400
Processing by ListsController#index as
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  List Load (0.3ms)  SELECT "lists".* FROM "lists"
  Rendered lists/index.html.erb within layouts/application (2.3ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 181ms (Views: 169.2ms | ActiveRecord: 0.9ms)
VS显示该用户的列表

Started GET "/boards" for ::1 at 2016-10-12 14:26:33 -0400
Processing by BoardsController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Board Load (0.1ms)  SELECT "boards".* FROM "boards" WHERE "boards"."user_id" = ?  [["user_id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered boards/index.html.erb within layouts/application (3.4ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 168ms (Views: 166.1ms | ActiveRecord: 0.2ms)
Started GET "/lists.3" for ::1 at 2016-10-12 14:28:26 -0400
Processing by ListsController#index as
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  List Load (0.3ms)  SELECT "lists".* FROM "lists"
  Rendered lists/index.html.erb within layouts/application (2.3ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 181ms (Views: 169.2ms | ActiveRecord: 0.9ms)
相关代码

型号

board.rb

    class Board < ActiveRecord::Base
  belongs_to :owner, class_name: "User", foreign_key: "user_id"

  has_many :lists
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :lists, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :owner

  def is_private?
    return self.is_private
  end
end
class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards

  has_one :board, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :board
end
class User < ActiveRecord::Base
  has_many :boards
  has_many :lists
  has_many :cards
  has_many :memberships
  has_many :board_memberships, :through => :memberships, :source => :board
  has_many :list_memberships, :through => :memberships, :source => :board

  has_secure_password

  validates :password,
    length: { :minimum => 8 },
    presence: true

  validates :username,
    uniqueness: true,
    presence: true

  validates_confirmation_of :password
end
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, presence: true, uniqueness: { scope: :board, message: "User is already a member of that board" }
  validates :board, presence: true
end
classboard:成员身份,:来源=>:用户
拥有多个:列表,:至=>:会员资格,:来源=>:董事会
验证是否存在:title
验证:所有者的存在
def是私人的吗?
返回self.u是私有的
结束
结束
list.rb

    class Board < ActiveRecord::Base
  belongs_to :owner, class_name: "User", foreign_key: "user_id"

  has_many :lists
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :lists, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :owner

  def is_private?
    return self.is_private
  end
end
class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards

  has_one :board, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :board
end
class User < ActiveRecord::Base
  has_many :boards
  has_many :lists
  has_many :cards
  has_many :memberships
  has_many :board_memberships, :through => :memberships, :source => :board
  has_many :list_memberships, :through => :memberships, :source => :board

  has_secure_password

  validates :password,
    length: { :minimum => 8 },
    presence: true

  validates :username,
    uniqueness: true,
    presence: true

  validates_confirmation_of :password
end
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, presence: true, uniqueness: { scope: :board, message: "User is already a member of that board" }
  validates :board, presence: true
end
类列表:会员资格,:来源=>:董事会
验证是否存在:title
验证是否存在:电路板
结束
user.rb

    class Board < ActiveRecord::Base
  belongs_to :owner, class_name: "User", foreign_key: "user_id"

  has_many :lists
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :lists, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :owner

  def is_private?
    return self.is_private
  end
end
class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards

  has_one :board, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :board
end
class User < ActiveRecord::Base
  has_many :boards
  has_many :lists
  has_many :cards
  has_many :memberships
  has_many :board_memberships, :through => :memberships, :source => :board
  has_many :list_memberships, :through => :memberships, :source => :board

  has_secure_password

  validates :password,
    length: { :minimum => 8 },
    presence: true

  validates :username,
    uniqueness: true,
    presence: true

  validates_confirmation_of :password
end
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, presence: true, uniqueness: { scope: :board, message: "User is already a member of that board" }
  validates :board, presence: true
end
class用户:成员身份,:来源=>:董事会
拥有多个:列表成员身份,:至=>:成员身份,:来源=>:董事会
有安全的密码吗
验证:密码,
长度:{:最小=>8},
存在:真实
验证:用户名,
独特性:没错,
存在:真实
验证密码的确认
结束
会员资格。rb

    class Board < ActiveRecord::Base
  belongs_to :owner, class_name: "User", foreign_key: "user_id"

  has_many :lists
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :lists, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :owner

  def is_private?
    return self.is_private
  end
end
class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards

  has_one :board, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :board
end
class User < ActiveRecord::Base
  has_many :boards
  has_many :lists
  has_many :cards
  has_many :memberships
  has_many :board_memberships, :through => :memberships, :source => :board
  has_many :list_memberships, :through => :memberships, :source => :board

  has_secure_password

  validates :password,
    length: { :minimum => 8 },
    presence: true

  validates :username,
    uniqueness: true,
    presence: true

  validates_confirmation_of :password
end
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, presence: true, uniqueness: { scope: :board, message: "User is already a member of that board" }
  validates :board, presence: true
end
类成员资格
控制器

boards\u controller.rb(我只在这里添加了相关部分)

类BoardsController“未找到板或无法访问”,然后返回
结束
结束
def授权联合会会员资格
返回
如果@board.u是私人的?
如果!证明…是真实的
将\重定向到根\ url,:notice=>“请先登录”并返回
结束
如果@board.owner!=当前用户| |@董事会成员。包括?(当前用户)
将_重定向到root_url,:notice=>“未找到板或无法访问”,然后返回
结束
结束
结束
#邮政局
#POST/boards.json
def创建
@线路板=线路板。新建(线路板参数)
@board.owner=当前用户
回应待办事项|格式|
如果@board.save
format.html{将_重定向到@board,注意:'board已成功创建。}
format.json{render:show,status::created,location:@board}
其他的
format.html{render:new}
format.json{render json:@board.errors,status::unprocessable_entity}
结束
结束
结束
列出\u controller.rb(整个文件)

class ListsController