前提・実現したいこと
railsで簡易的なSNSを作っておりまして、どのページでも常にユーザーの情報を編集できるサイドバーのようなものを配置しようとしたら、showページでは問題なかったので、そのままindexページでコードをコピペしたときにエラーが発生しました。
このエラーの場合どこが問題なのでしょうか、
どなたかご教授お願いいたします。
ActionView::Template::Error (undefined method `name' for nil:NilClass):
該当のソースコード
ruby on rails #app/views/users/index.html.erb <div class = "container"> <div class="row"> <div class="col-sm-3"> <div class="profile-container"> <h2>User Info</h2> <%= attachment_image_tag @user, :profile_image, :fill, 60, 60, format: 'jpeg', class: "img-circle pull-left profile-img", fallback: "no_image.jpg" %> <h3 class="profile-name"><%= @user.name %></h3> <h3 class="profile-introduction"><%= @user.introduction %></h3> <% if @user.id == current_user.id %> <p><%= link_to "プロフィール編集", edit_user_path(@user), class: "pull-right" %></p> <% end %> </div> <div> <h2>New Book</h2> <%= form_for @book do |f| %> <div> <%= f.label :title, 'title' %> </div> <div> <%= f.text_field :title %> </div> <div> <%= f.label :body, '書評' %> </div> <div> <%= f.text_area :body %> </div> <%= f.submit '登録' %> <% end %> </div> </div> </div> </div>
class UsersController < ApplicationController before_action :authenticate_user! def index @users = User.all @user = User.find(params[:id]) end def new end def edit @user = User.find(params[:id]) end def show @books = Book.all @user = User.find(params[:id]) @book = Book.new end def create_book p params p book_params book = Book.new(book_params) book.user_id = current_user.id book.save redirect_to user_path(current_user.id) end def update @user = User.find(params[:id]) @user.update(user_params) redirect_to user_path(@user.id) end private def book_params params.require(:book).permit(:title, :body) end def user_params params.require(:user).permit(:name, :profile_image, :introduction) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/05/26 03:09