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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1895閲覧

NoMethodError in Users#indexを解決したい

veludo

総合スコア3

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/30 09:50

前提・実現したいこと

Rails勉強中のプログラミング初心者です。
Railsでwebアプリケーションを作成中です。
books/index.html.erbファイル・books/show.html.erbファイル・users/index.html.erbファイル・users/show.html.erbファイルのコードを部分テンプレートを利用してまとめようとしたところ、下記のエラーが出てしまい、先へ進めずにいます。

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

NoMethodError in Users#index undefined method `rendar' for #<#<Class:0x00007f3e04849470>:0x00007f3e044e5c70>

ターミナルに表示されているエラーメッセージ

ActionView::Template::Error (undefined method `rendar' for #<#<Class:0x00000000058a8108>:0x00007f4eba99d3f0> Did you mean? render): 1: <%= rendar '/users/userinfo', user: @user, book: @book %> 2: 3: 4: <% if user_signed_in? %> app/views/users/index.html.erb:1:in `_app_views_users_index_html_erb__3296113570933845544_69988057411200'

users/_userinfo.html.erb 部分テンプレートファイル

html

1<div class="container"> 2 <div class="row"> 3 <div class="col-xs-3"> 4 <h2>User info</h2> 5 6 <%= attachment_image_tag user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg" %> 7 8 <table class="table"> 9 <thead> 10 <th></th> 11 <th></th> 12 </thead> 13 <tr> 14 <td>name:</td> 15 <td><%= user.name %></td> 16 </tr> 17 18 <tr> 19 <td>introduction:</td> 20 <td><%= user.introduction %></td> 21 </tr> 22 23 <tr><% if user.id == current_user.id %></tr> 24 <% end %> 25 </table> 26 27 <% if @user.id == current_user.id %> 28 <%= link_to " ", edit_user_path(user), do %> 29 <li class="fas fa-user-cog"></li> 30 <% end %> 31 <% end %> 32 33 <h1>New book</h1> 34 <%= form_with model:@book, local:true do |f| %> 35 <h4>Title</h4> 36 <%= f.text_field :title %> 37 <h4>Opinion</h4> 38 <%= f.text_area :body %><br> 39 <%= f.submit 'Create book', class: "btn btn-success" %> 40 <% end %> 41 </div> 42 </div> 43</div>

books/index.html.erb

html

1<%= rendar '/users/userinfo', user: @user, book: @book %> 2 3 4<h2>Books</h2> 5 6<% @books.each do |f| %> 7 <%= link_to attachment_image_tag(f.user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg"), user_path(f.user.id) %> 8 9 <%= link_to f.title, book_path(f.id) %> 10 <%= f.body %><br> 11<% end %>

books/show.html.erb

html

1<%= link_to attachment_image_tag(@user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg"),user_path(@user.id) %> 2<p><%= link_to @user.name, user_path(@user.id) %></p> 3 4 5<h2>Title:</h2> 6<p><%= link_to @book.title, book_path(@book.id) %></p> 7<p><%= @book.title %></p> 8<h2>Body:</h2> 9<p><%= @book.body %></p> 10 <% if @user.id == current_user.id %> 11 <%= link_to "Edit", edit_book_path(@book.id) %></p> 12 <%= link_to "Destroy", book_path(@book.id) , method: :delete, "data-confirm" => "Are you sure?" %> 13 <% end %> 14 15<%= rendar '/users/userinfo', user: @user, book: @book %> 16

users/index.html.erb

html

1<%= rendar '/users/userinfo', user: @user, book: @book %> 2 3 4<% if user_signed_in? %> 5 6 <h1>Users</h1> 7 8 <% @users.each do |f| %> 9 <%= attachment_image_tag f, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg" %> 10 <%= f.name %> 11 <%=link_to "Show", user_path(f.id) %> 12 <% end %> 13 14 <% else %> 15 <h1>welcome to 16 <i class="fas fa-book"></i> 17 <strong>Bookers</strong> 18 !! 19 </h1> 20 <p>In 21 <i class="fas fa-book"></i> 22 <strong>Bookers</strong> 23 , 24 </p> 25 <p>you can share and exchange your opinions , inpressions , and emotions</p> 26 <p> about various books!</p> 27 28<% end %> 29

users/show.html.erb

html

1<div class="col-xs-9"> 2 <h2>Books</h2> 3 <table class="table table-hover table-inverse"> 4 <thead> 5 <th> </th> 6 <th>title</th> 7 <th>body</th> 8 </thead> 9 <% @books.each do |f| %> 10 <tr> 11 <td><%= link_to attachment_image_tag(f.user, :profile_image, :fill, 100, 100, format: 'jpeg', fallback: "no_image.jpg"),user_path(f.user.id) %></td> 12 13 <td><%= link_to f.title, book_path(f.id) %></td> 14 <td><%= f.body %></td> 15 <% end %> 16 </tr> 17 </table> 18</div> 19 20<%= rendar '/users/userinfo', user: @user, book: @book %> 21

userscontroller.rb

ruby

1class UsersController < ApplicationController 2 3 def index 4 @user = current_user 5 6 @users = User.all 7 @book = Book.new 8 @books = Book.all 9 end 10 11 def edit 12 @user = User.find(params[:id]) 13 end 14 15 def show 16 @user = User.find(params[:id]) 17 @book = Book.new 18 @books = @user.books 19 20 end 21 22 def update 23 @user = User.find(params[:id]) 24 @user.update(user_params) 25 redirect_to user_path(@user.id) 26 end 27 28 def create 29 end 30 31 private 32 def user_params 33 params.require(:user).permit(:name, :profile_image, :introduction) 34 end 35 36 37end 38

bookscontroller.rb

ruby

1class BooksController < ApplicationController 2 3 def new 4 end 5 6 def create 7 @book = Book.new(book_params) 8 @book.user_id = current_user.id 9 10 @book.save 11 redirect_to book_path(@book.id) 12 end 13 14 def index 15 @user = current_user 16 17 18 @book = Book.new 19 @books = Book.all 20 end 21 22 def show 23 @book = Book.find(params[:id]) 24 @books = Book.all 25 26 @user = @book.user 27 28 @booknew = Book.new 29 end 30 31 def edit 32 @book = Book.find(params[:id]) 33 render layout: false 34 end 35 36 def update 37 @book = Book.find(params[:id]) 38 @book.update(book_params) 39 40 redirect_to book_path(@book.id) 41 end 42 43 def destroy 44 @book = Book.find(params[:id]) 45 @book.destroy 46 redirect_to books_path 47 48 end 49 50 private 51 def book_params 52 params.require(:book).permit(:title, :body) 53 54 end 55 56 def user_params 57 params.require(:user).permit(:name, :profile_image, :introduction) 58 end 59 60end 61

###applicationcontroller.rb

ruby

1<!DOCTYPE html> 2<html> 3 <head> 4 <title>Bookers2</title> 5 <%= csrf_meta_tags %> 6 <%= csp_meta_tag %> 7 8 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 9 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 10 </head> 11 12 <body> 13 <div class="navbar navbar-inverse"> 14 <div class="navbar-left text-muted" > 15 <h2 class="title">Bookers</h2> 16 </div> 17 <ul class="nav navbar-nav navbar-right" data-hover="dropdown" data-animations="zoomIn fadeIn fadeInUp fadeIn"> 18 <% if user_signed_in? %> 19 <li class=""> 20 <%= link_to 'Home', user_path(@user.id) do %> 21 <li class="fas fa-home"></li> 22 <% end %> 23 </li> 24 <li class=""> 25 <%= link_to 'Users', users_path do %> 26 <li class="fas fa-users"></li> 27 <% end %> 28 </li> 29 <li class=""> 30 <%= link_to 'Books', books_path do %> 31 <li class="fas fa-book-open"></li> 32 <% end %> 33 </li> 34 <li class=""> 35 <%= link_to "logout", destroy_user_session_path, method: :delete do %> 36 <li class="fas fa-sign-out-alt"></li> 37 <% end %> 38 </li> 39 40 <% else %> 41 <li class=""> 42 <%= link_to 'Home', user_path(@user.id) do %> 43 <li class="fas fa-home"></li> 44 <% end %> 45 </li> 46 <li class=""> 47 <%= link_to "About", about_path do %> 48 <li class="fas fa-link"></li> 49 <% end %> 50 </li> 51 <li class=""> 52 <%= link_to "sign up", new_user_registration_path do %> 53 <li class="fas fa-user-plus"></li> 54 <% end %> 55 </li> 56 <li class=""> 57 <%= link_to "Login", new_user_session_path do %> 58 <li class="fas fa-sign-in-alt"></li> 59 <% end %> 60 </li> 61 <% end %> 62 </ul> 63 </div> 64 <%= yield %> 65 <footer></footer> 66 </body> 67</html> 68

試したこと

スペルミス等のチェックも行いましたが、見当たりませんでした。


理解が浅く、見落としている箇所が多くあるかもしれません。
どなたかご教授いただけると幸いです。説明不足で申し訳ありませんが、よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

s/rendar/render/

投稿2021/03/30 10:07

winterboum

総合スコア23347

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

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

veludo

2021/03/30 10:16

ご回答ありがとうございます。 すっかり見落としていました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問