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

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

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

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

2回答

565閲覧

誤字rails tutorial 第13章 13.3.5 リスト13,57テスト時のエラー home.html.erbでパーシャルを使用した場合

_on_my_manaita

総合スコア5

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/09/11 20:02

編集2020/09/12 14:43

現在rails tutorialの13章を学習しています.
そこの「13.3.5フィード画面のマイクロポストをテストする」のリスト 13.57のテストでエラーが出てしまいます.
どうやら,home.html.erbでパーシャルを使用しなければテストはパスできるのですが,その理由がわかりません.
どうか,教えてください…

発生している問題・エラーメッセージ

エラーメッセージ ActionView::Template::Error: ActionView::Template::Error: Missing partial microposts/_user_logged_in, application/_user_logged_in with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "/home/ubuntu/environment/sample_app/app/views" * "/home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actiontext-6.0.3/app/views" * "/home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionmailbox-6.0.3/app/views" app/views/static_pages/home.html.erb:2 app/controllers/microposts_controller.rb:12:in `create' test/integration/microposts_interface_test.rb:15:in `block (2 levels) in <class:MicropostsInterfaceTest>' test/integration/microposts_interface_test.rb:14:in `block in <class:MicropostsInterfaceTest>'

###他参考コード
micropost_controller.rb

class

1 before_action :logged_in_user, only: [:create, :destroy] 2 before_action :correct_user, only: :destroy 3 4 def create 5 @micropost = current_user.microposts.build(micropost_params) 6 if @micropost.save 7 flash[:success] = "Micropost created!" 8 redirect_to root_url 9 else 10 @feed_items = current_user.feed.paginate(page: params[:page]) 11 render 'static_pages/home' 12 end 13 end 14151617

microposts_interface_test

require 'test_helper' class MicropostsInterfaceTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end test "micropost interface" do log_in_as(@user) get root_path assert_select 'div.pagination' # 無効な送信 assert_no_difference 'Micropost.count' do post microposts_path, params: { micropost: { content: "" } } end assert_select 'div#error_explanation' assert_select 'a[href=?]', '/?page=2' # 正しいページネーションリンク # 有効な送信 content = "This micropost really ties the room together" assert_difference 'Micropost.count', 1 do post microposts_path, params: { micropost: { content: content } } end assert_redirected_to root_url follow_redirect! assert_match content, response.body # 投稿を削除する assert_select 'a', text: 'delete' first_micropost = @user.microposts.paginate(page: 1).first assert_difference 'Micropost.count', -1 do delete micropost_path(first_micropost) end # 違うユーザーのプロフィールにアクセス(削除リンクがないことを確認) get user_path(users(:archer)) assert_select 'a', text: 'delete', count: 0 end end

_user_info.html.erb

<%= link_to gravatar_for(current_user, size: 50), current_user %> <h1><%= current_user.name %></h1> <span><%= link_to "view my profile", current_user %></span> <span><%= pluralize(current_user.microposts.count, "micropost") %></span>

_micropost_form.html.erb

<%= form_with(model: @micropost, local: true) do |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="field"> <%= f.text_area :content, placeholder: "Compose new micropost..." %> </div> <%= f.submit "Post", class: "btn btn-primary" %> <span class="image"> <%= f.file_field :image %> </span> <% end %>

_feed.html.erb

<% if @feed_items.any? %> <ol class="microposts"> <%= render @feed_items %> </ol> <%= will_paginate @feed_items, params: { controller: :static_pages, action: :home } %> <% end %>

エラーのでるソースコード

/sample_app/app/views/static_pages/home.html.erb

<% if logged_in? %> <%= render 'user_logged_in' %> <% else %> <%= render 'user_not_logged_in' %> <% end %>

/sample_app/app/views/static_pages/_user_logged_in.html.erb

<div class="row"> <aside class="col-md-4"> <section class="user_info"> <%= render 'shared/user_info' %> </section> <section class="micropost_form"> <%= render 'shared/micropost_form' %> </section> </aside> <div class="col-md-8"> <h3>Micropost Feed</h3> <%= render 'shared/feed' %> </div> </div>

/sample_app/app/views/static_pages/_user_not_logged_in.html.erb

<div class="center jumbotron"> <h1>Welcome to the Sample App</h1> <h2> This is the home page for the <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a> sample application. </h2> <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %> </div>

###エラーのでないソースコード
/sample_app/app/views/static_pages/home.html.erb

<% if logged_in? %> <div class="row"> <aside class="col-md-4"> <section class="user_info"> <%= render 'shared/user_info' %> </section> <section class="micropost_form"> <%= render 'shared/micropost_form' %> </section> </aside> <div class="col-md-8"> <h3>Micropost Feed</h3> <%= render 'shared/feed' %> </div> </div> <% else %> <div class="center jumbotron"> <h1>Welcome to the Sample App</h1> <h2> This is the home page for the <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a> sample application. </h2> <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %> </div> <%= link_to image_tag("rails.svg", alt: "Rails logo", width: "200px"), "https://rubyonrails.org/" %> <% end %>

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

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

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

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

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

unhappychoice

2020/09/12 10:16

現状の _user_logged_in.html.erb のフルパスを記入してくださいmm
_on_my_manaita

2020/09/12 14:40

/sample_app/app/views/static_pages/_user_logged_in.html.erb です!
guest

回答2

0

ベストアンサー

render 'user_logged_in' と呼び出しているので、

/sample_app/app/views/static_pages/_user_logged_in.html.erb

ではなく、

/sample_app/app/views/_user_logged_in.html.erb

に配置する必要があります。(エラー文をよく読めばその事がわかるはず。
もしくは、 views/ 直下にパーシャルを置くのは、整理ができないため

  • /sample_app/app/views/shared/_user_logged_in.html.erb に配置
  • render 'shared/user_logged_in' に書き換え

するとよいかと思います。

投稿2020/09/12 14:45

編集2020/09/12 14:48
unhappychoice

総合スコア1531

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

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

_on_my_manaita

2020/09/12 14:57

解決しました!お恥ずかしいミスに回答いただきありがとうございます????????‍♂️
guest

0

file名が違います。
partialのfileは、呼ばれるfile名の前に "_"が必要です
_home.html.erb に改名してください

しつれい、早とちりしました
_user_logged_in ですね。
/home/ubuntu/environment/sample_app/app/views/microposts/_user_logged_in.html.erb
があるのに、そのエラーですか?

投稿2020/09/11 22:01

編集2020/09/11 22:09
winterboum

総合スコア23347

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

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

_on_my_manaita

2020/09/11 22:05

home.html.erbは呼び出すファイルではありません.
_on_my_manaita

2020/09/11 22:38

テストのコードも追加しておきました????
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問