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

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

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

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

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

Q&A

解決済

3回答

449閲覧

Unable to autoload constant Blog_content, expeceted 〜(ファイルの位置) to define it = モデル内のクラスをautoloadできません

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

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

0グッド

0クリップ

投稿2019/06/26 11:40

編集2019/06/27 17:20

###作ろうとしている物
ブログ投稿サイト

###困っていること
ユーザーの投稿一覧画面から、タイトルをクリックすることで投稿詳細ページに飛ばしたい。が、できない。

###発生しているエラー

chorome

1uninitialized constant BlogsController::Blog_contents 2 3@blog = Blog_contents.find_by(id: params[:id]) 4→コントローラのこの部分に赤線

###理解度と試していること
1、投稿一覧
飛ばす際に、paramsにクリックした投稿のid番号をパラメータとして付加し、コントローラのshowアクションに飛ばす。
※前提
投稿一覧画面では、インスタンス変数に@blogを用いています。(views/blogs/index.html.erb)

<div class="contents"> <% @blog.each do |content|%> <p> <%= link_to(content.title_name, "/blogs/#{content.id}") %> </p> <% end %> </div>

2、ルーティング(config/routes.rb)

Rails.application.routes.draw do root to: 'pages#index' resources :blogs devise_for :users, controllers:{ regisrations: 'users/regisrations', sessions: 'users/sessions' } # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end

補足:rake routesの結果

terminal

1 Prefix Verb URI Pattern Controller#Action 2 root GET / pages#index 3 blogs GET /blogs(.:format) blogs#index 4 POST /blogs(.:format) blogs#create 5 new_blog GET /blogs/new(.:format) blogs#new 6 edit_blog GET /blogs/:id/edit(.:format) blogs#edit 7 blog GET /blogs/:id(.:format) blogs#show 8 PATCH /blogs/:id(.:format) blogs#update 9 PUT /blogs/:id(.:format) blogs#update 10 DELETE /blogs/:id(.:format) blogs#destroy 11 new_user_session GET /users/sign_in(.:format) users/sessions#new 12 user_session POST /users/sign_in(.:format) users/sessions#create 13 destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy 14 new_user_password GET /users/password/new(.:format) devise/passwords#new 15 edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 16 user_password PATCH /users/password(.:format) devise/passwords#update 17 PUT /users/password(.:format) devise/passwords#update 18 POST /users/password(.:format) devise/passwords#create 19 cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel 20 new_user_registration GET /users/sign_up(.:format) devise/registrations#new 21 edit_user_registration GET /users/edit(.:format) devise/registrations#edit 22 user_registration PATCH /users(.:format) devise/registrations#update 23 PUT /users(.:format) devise/registrations#update 24 DELETE /users(.:format) devise/registrations#destroy 25 POST /users(.:format) devise/registrations#create 26 rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show 27rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show 28 rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show 29update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update 30 rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create 31

3、コントローラのshowアクション
paramsで付加したid番号から投稿を探し出して、インスタンス変数に代入する。(app/controller/blogs_controller)

Rails5

1 2class BlogsController < ApplicationController 3 4 before_action :authenticate_user! 5 #showを省いた 6 before_action :set_blog, only: [:edit, :update, :destroy] 7 8 9ここの部分。 10ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 11 def index 12 @blog = current_user.blog_contents 13 end 14 15 def show 16 @blog = Blog_contents.find_by(id: params[:id]) 17   →ここのコードに関してエラーが出ます。 18 end 19ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 20 def new 21 @blog = current_user.blog_contents.build 22 end 23 24 def edit 25 end 26 27 def create 28 @blog = current_user.blog_contents.build(blog_params) 29 30 respond_to do |format| 31 if @blog.save 32 format.html { redirect_to blogs_path, notice: 'Note was successfully created.' } 33 format.json { render :show, status: :created, location: @blog } 34 else 35 format.html { render :new } 36 format.json { render json: @blog.errors, status: :unprocessable_entity } 37 end 38 end 39 end 40 41 def update 42 respond_to do |format| 43 if @blog.update(note_params) 44 format.html { redirect_to @blog, notice: 'Note was successfully updated.' } 45 format.json { render :show, status: :ok, location: @blog } 46 else 47 format.html { render :edit } 48 format.json { render json: @blog.errors, status: :unprocessable_entity } 49 end 50 end 51 end 52 53 def destroy 54 @blog.destroy 55 respond_to do |format| 56 format.html { redirect_to notes_url, notice: 'Note was successfully destroyed.' } 57 format.json { head :no_content } 58 end 59 end 60 61 private 62 def set_blog 63 @blog = current_user.blog_contents.find_by(id: params[:id]) 64 redirect_to root_path if @blog.nill? 65 end 66 67 def blog_params 68 params.require(:blog_content).permit(:title_name, :content) 69 end 70end

4、投稿詳細画面
インスタンス変数に代入された投稿のタイトルと、中身を表示する。(app/views/show.html.erb)

Rails5

1<div class="content"> 2 <%= @blog.title_name %> 3 <%= @blog.content %> 4</div>

5、ターミナルからログを確認した結果
投稿一覧画面から、id番号3の投稿をクリックし、詳細画面に飛ぼうとしている部分のログです。

terminal

1Started GET "/blogs/3" for ::1 at 2019-06-26 20:19:48 +0900 2Processing by BlogsController#show as HTML 3 Parameters: {"id"=>"3"} 4 User Load (3.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 5 ↳ /Users/horiguchihiroki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 6Completed 500 Internal Server Error in 13ms (ActiveRecord: 3.7ms) 7 8 9 10NameError (uninitialized constant BlogsController::Blog_contents): 11 12app/controllers/blogs_controller.rb:13:in `show'

###問題解決のために試したこと
エラー文をググってそのサイトに書いてあることを確認しました。
https://qiita.com/imotan/items/c73fab5ee230114a08b6
ファイルとファイルの中のクラスの名前が一致していないこと(アンダーバーがあるかないか等)が問題のようなのですが、問題ないと思われます。

上記のサイトに書いてあることを真似して、config/initializers/requirements.rbを作成し、その中に該当コードを書くも、今度は「rails s」ができなくなり、エラー解決のためのエラー解決になりつつあり、1時間以上を使用した割に全く解決の糸口に至らないので、こちらで質問しながら解決していこうと考えました。
よろしくお願いします。

###補足
ご回答ありがとうございます。
モデル名はblog_contentsです。

terminal

1mysql> show tables; 2+----------------------------+ 3| Tables_in_blog_development | 4+----------------------------+ 5| ar_internal_metadata | 6| →blog_contents | 7| schema_migrations | 8| users | 9+----------------------------+

ご指摘の通りに変更後、以下のエラーが表示されました。

chorome

1Unable to autoload constant Blog_content

blog_contentがautoroadできないようなので、その原因を取り払ってあげればいいのか?と考え、
{https://teratail.com/questions/87201}こちらの記事を参考に、修正箇所を探そうと思ったのですが、スペルミスは発生していないようなので、修正できずにいます。
引き続き原因究明をしますが、ご指摘いただけると嬉しいです。

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

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

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

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

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

guest

回答3

0

Railsの命名ルール的にテーブル名がblog_contentsであれば
モデルのクラス名はBlogContentになるかと思います。
app/models下にクラスがあるのでそこにあるクラス名を確認してみてください。

投稿2019/06/27 15:46

schindler

総合スコア112

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

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

退会済みユーザー

退会済みユーザー

2019/06/27 17:11 編集

ご回答ありがとうございます。ご推察の通り、モデルのなかのクラス名はBlogContentです。 実は、昨日から工夫して、目的とする動きを実現することができました。 その部分に関して自己解決したものを編集して載せておきます。 ただ、どうしてうまくいくのかいまいちわかっておりません。 そのため、別の質問としてまた書いていきたいと思います。 今回はご回答ありがとうございました。別質問が出来次第、リンクを貼らせていただこうと思います。 お時間あるときにご回答いただけますと幸いです。
guest

0

モデル名が何かわからないのですが、

current_user.blog_contents

でblogが取れているのであれば、

@blog = Blog_content.find_by(id: params[:id])

じゃないですかね?

投稿2019/06/27 01:12

t_kusakabe74

総合スコア549

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

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

退会済みユーザー

退会済みユーザー

2019/06/27 06:35

ご回答ありがとうございます。質問を編集いたしました。 ご確認お願い致します。
guest

0

ベストアンサー

###自己解決結果
app/controller/blogs_controller

Rails5

1 def show 2 @blog = BlogContent.find_by(id: params[:id]) 3 end

showアクションの中身を以下のように変更するとエラーが消え、無事に詳細画面にたどり着くことができるようになりました。
ただ、find_byメソッドを使えるのは、モデルの名前に対してのみだと考えており、なぜクラス名に対してこのような書き方ができるのかわかりません。
そちらの別質問に関するリンクを貼りますので、知見をお持ちの方是非ご教授ください。

投稿2019/06/27 17:20

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問