質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MongoDB

MongoDBはオープンソースのドキュメント指向データベースの1つです。高性能で、多くのリトルエンディアンシステムを利用することができます。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

1730閲覧

mongoidを使っているのですが、ユーザーページに行こうとするとエラーがでます。

drizzing20

総合スコア363

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MongoDB

MongoDBはオープンソースのドキュメント指向データベースの1つです。高性能で、多くのリトルエンディアンシステムを利用することができます。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/10/10 14:14

編集2016/10/10 14:17

railsでmongoidを使ってユーザーモデルを作りました。そこで
http://localhost:3000/users/1」とうってみたところ以下のようなエラーがでて解決できません。わかる方お願いします。
ユーザー1はもうクリエイトしてあります

エラー文
LoadError in UsersController#show
cannot load such file -- /app/controllers/models/user

users_controller.rb

1require_relative 'models/user'

app/controllers/users_controller.rb:4:in require_relative' app/controllers/users_controller.rb:4:in <top (required)>'
_
_
_
_

users_controller.rb

RUBY

1require_relative 'models/user' 2class UsersController < ApplicationController 3 def new 4 end 5 6 def show 7 @user = User.find(params[:id]) 8 end 9end 10

user.rb

RUBY

1require 'mongoid' 2require 'bcrypt' 3 4class User 5 include Mongoid::Document 6 include Mongoid::Timestamps 7 8 field :name, type: String 9 field :email, type: String 10 field :password, type: String 11 field :password_hash 12 field :password_salt 13 14 attr_readonly :password_hash, :password_salt 15 16 17 validates :name, presence: true, length: { maximum: 50 } 18 validates :name, uniqueness: true 19 validates :email, uniqueness: true, length: { maximum: 255 } 20 validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, on: :create } 21 validates :email, presence: true 22 validates :password_hash, confirmation: true 23 validates :password_hash, presence: true 24 validates :password_salt, presence: true 25 26 # パスワードの暗号化にはBCrypt::Engineを使って、不可逆に行う。ランダムなsalt値を生成してパスワードのハッシュ化を行い、DBへ保存する。 27 def encrypt_password(password) 28 if password.present? 29 self.password_salt = BCrypt::Engine.generate_salt 30 self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 31 end 32 end 33 34 # DBに保存されたパスワードと入力されたパスワードが一致した場合userを返す 35 def self.authenticate(email, password) 36 user = self.where(email: email).first 37 if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) 38 user 39 else 40 nil 41 end 42 end 43 44end 45 46``` 47 48エラー改善のために、require_relative 'models/user'の文を削除してみましたが、 @user = User.find(params[:id])にエラーが出ました 49 50そのエラーがこちら 51``` 52Mongoid::Errors::DocumentNotFound in UsersController#show 53message: Document(s) not found for class User with id(s) 4. summary: When calling User.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 4 ... (1 total) and the following ids were not found: 4. resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples. 54 55 @user = User.find(params[:id]) 56```

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

root設定が間違えていました解決しました。

投稿2016/10/11 13:31

drizzing20

総合スコア363

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問