简体   繁体   English

没有ID的用户无法找到-Ruby 2.1.4 / Rails 4.1.6

[英]Couldn't find User without an ID - Ruby 2.1.4 / Rails 4.1.6

I am facing an error while trying to link the :username in my User table and my Room table. 尝试在User表和Room表中链接:username时遇到错误。 I made a custom auth with devise and added :username . 我使用devise进行了自定义身份验证,并添加了:username

I would like the username to be the link between the User table from devise and my Room table. 我希望username是来自devise的User表和Room表之间的链接。

I am trying to build this app to recreate a kind of airbnb but mainly as an exercise as I started programming in ruby few months ago. 我正在尝试构建该应用程序以重新创建一种Airbnb,但主要是作为几个月前我开始在ruby中进行编程时的一项练习。

I get the error: 我得到错误:

ActiveRecord::RecordNotFound in RoomsController#new
Couldn't find User without an ID
line #19 @room.username = User.find(params[:username])

Thank you very much for your help. 非常感谢您的帮助。 I am stuck in here for hours now :-( 我在这里呆了几个小时:-(

rooms_controller rooms_controller

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

routes.rb 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

room.rb room.rb

class Room < ActiveRecord::Base
  mount_uploader :photo, PictureUploader
  belongs_to :user
  validates_presence_of :username, :location, :description, :capacity, :price_day, :photo
end

user.rb user.rb

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

It should be something like this 应该是这样的

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

If you just use .find() it expects the id of the user. 如果只使用.find(),则它需要用户的ID。 Also see http://guides.rubyonrails.org/active_record_querying.html#dynamic-finders 另请参见http://guides.rubyonrails.org/active_record_querying.html#dynamic-finders

There is a logic error in that you are saving @room.username to the User Object. 将@ room.username保存到用户对象存在逻辑错误。 You should be setting @room.user = User.find_by(...) OR @room.user_id = User.find_by(...).id 您应该设置@ room.user = User.find_by(...)或@ room.user_id = User.find_by(...)。id

Active record will automagically make a method for you that will be @room.user.username if you want to get the username. 如果要获取用户名,活动记录将自动为您创建一个方法,该方法为@ room.user.username。

Now, here are the ways to find a user. 现在,这是查找用户的方法。

@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.

As already mentioned in the answers, User.find() takes an ID. 正如答案中已经提到的那样,User.find()需要一个ID。 One thing to know is that all methods that start with .find for active record return a single record even if many meet the condition. 要知道的一件事是,以.find开头的用于活动记录的所有方法都返回一条记录,即使许多满足条件的记录也是如此。

If you are having any problems still, then show us your Database Schema, and we can help further. 如果仍然有任何问题,请向我们展示您的数据库架构,我们将为您提供进一步的帮助。

I found a solution. 我找到了解决方案。 The Room is created with the the right :username and nothing is seen by the user. 使用正确的:username创建会议室,但用户看不到任何内容。 In my Rooms controller, I kept 在我的房间控制器中,

def new
@room = Room.new end

And I added this line in the "def create" part : 我在“ def create”部分添加了这一行:

def create
@room = Room.new(room_params)
@room.username = current_user.username

Thank you for your help, this help me to understand better the relations in rails. 感谢您的帮助,这有助于我更好地了解Rails中的关系。

Have a nice day ! 祝你今天愉快 !

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

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