Ruby on rails 不能';找不到没有ID的用户-Ruby 2.1.4/Rails 4.1.6

Ruby on rails 不能';找不到没有ID的用户-Ruby 2.1.4/Rails 4.1.6,ruby-on-rails,ruby,activerecord,devise,params,Ruby On Rails,Ruby,Activerecord,Devise,Params,尝试链接我的用户表和我的房间表中的:用户名时遇到错误。我用Desive定制了一个auth,并添加了:username 我希望用户名成为designe的用户表和我的房间表之间的链接 我正在尝试构建这个应用程序来重新创建一种airbnb,但主要是作为一种练习,因为我几个月前开始用ruby编程 我得到一个错误: ActiveRecord::RecordNotFound in RoomsController#new Couldn't find User without an ID line #19 @r

尝试链接我的
用户
表和我的
房间
表中的
:用户名
时遇到错误。我用Desive定制了一个auth,并添加了
:username

我希望
用户名
成为designe的
用户
表和我的
房间
表之间的链接

我正在尝试构建这个应用程序来重新创建一种airbnb,但主要是作为一种练习,因为我几个月前开始用ruby编程

我得到一个错误:

ActiveRecord::RecordNotFound in RoomsController#new
Couldn't find User without an ID
line #19 @room.username = User.find(params[:username])
非常感谢你的帮助。我被困在这里好几个小时了:-(

房间\u控制器

def new
  @room = Room.new
  @room.username = User.find(params[:username]) #error seems to come from here
end
routes.rb

Rails.application.routes.draw do
  devise_for :users
  get "home/info"

  root :to => "home#info"

  resources :rooms

  resources :users do
  resources :rooms
end
class Room < ActiveRecord::Base
  mount_uploader :photo, PictureUploader
  belongs_to :user
  validates_presence_of :username, :location, :description, :capacity, :price_day, :photo
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :username
  validates_uniqueness_of :username

  has_many :rooms
end
room.rb

Rails.application.routes.draw do
  devise_for :users
  get "home/info"

  root :to => "home#info"

  resources :rooms

  resources :users do
  resources :rooms
end
class Room < ActiveRecord::Base
  mount_uploader :photo, PictureUploader
  belongs_to :user
  validates_presence_of :username, :location, :description, :capacity, :price_day, :photo
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :username
  validates_uniqueness_of :username

  has_many :rooms
end
教室
user.rb

Rails.application.routes.draw do
  devise_for :users
  get "home/info"

  root :to => "home#info"

  resources :rooms

  resources :users do
  resources :rooms
end
class Room < ActiveRecord::Base
  mount_uploader :photo, PictureUploader
  belongs_to :user
  validates_presence_of :username, :location, :description, :capacity, :price_day, :photo
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :username
  validates_uniqueness_of :username

  has_many :rooms
end
class用户
应该是这样的

def new
  @room = Room.new
  @room.username = User.find_by_username(params[:username])
end

如果您只使用.find(),它需要用户的id。另请参见,在将@room.username保存到用户对象时存在逻辑错误。您应该设置@room.user=user.find_by(…)或@room.user_id=user.find_by(…).id

如果您想获取用户名,Active record将自动为您创建一个@room.user.username方法

现在,以下是找到用户的方法

@room = Room.new #Then either of the following

@room.user = User.find_by(username: params[:username]) #Returns only one value

@room.user = User.find_by_username(params[:username])  #Returns only one value

@room.user = User.where(username: params[:username])   #Returns all users which meet condition.
正如答案中已经提到的,User.find()接受一个ID。需要知道的一点是,所有以.find开头的活动记录方法都返回一条记录,即使其中许多方法满足条件


若您仍然有任何问题,请向我们展示您的数据库架构,我们可以进一步提供帮助。

我找到了一个解决方案。文件室是使用正确的用户名创建的,用户看不到任何内容。 在我的房间里,我一直

def new
@room = Room.new end
我在“def create”部分添加了这一行:

感谢您的帮助,这有助于我更好地了解rails中的关系


祝你有一个愉快的一天!

我认为OP对RoR来说是新的。你也应该用可能的变化来更新你的答案:即
find_by(conditions)
where(conditions).last
我认为答案不应该包括rails文档的副本。这就是为什么a包含一个链接。他对rails绝对是新手,而且他尝试做的事情似乎没有多大意义,但为了帮助他,他需要更新他的问题