現在複数のWebページでrailsの勉強をしています。
そこでユーザの名前の変更ができる機能とユーザを一覧表示させるのではなく、個人の名前やメールアドレスは個人しか見えないようにしたいと思っています。
現在のエラーは、NoMethodError in Users::Configurations#index
と出ており、undefined method `name' for nil:NilClass
と言われています。
そして、ユーザの一覧表示は出来るのですがこのままでは自分自身以外のユーザの情報や編集、削除もできてしまうので個人の情報しか見えないようにしたいです。自分の書いたコードのコントローラーのindexメソッドであるUser.find_by_id(params[:user_id])にしています。
どのようにすればいいでしょうか?
ご回答お願いします。
以下にソースコードを載せておきます。
edit.html.erb
1<h1>Users::Configurations#edit</h1> 2<p>Find me in app/views/users/configurations/edit.html.erb</p> 3<h1>Editing User</h1> 4 5<%= render 'form' %> 6 7<%= link_to 'Show', @user %> | 8<%= link_to 'Back', users_path %>
index.html.erb
1<h1>Users::Configurations#index</h1> 2<p>Find me in app/views/users/configurations/index.html.erb</p> 3<p id="notice"><%= notice %></p> 4 5<h1>Listing Users</h1> 6 7<table> 8 <thead> 9 <tr> 10 <th>Name</th> 11 </tr> 12 </thead> 13 14 <tbody> 15 <% @user %> 16 <tr> 17 <td><%= @user.name %></td> 18 <td><%= link_to 'Show', @user %></td> 19 <td><%= link_to 'Edit', edit_@user_path(user) %></td> 20 <td><%= link_to 'Destroy', @user, method: :delete, data: { confirm: '本当に退会してもよろしいですか?' } %></td> 21 </tr> 22 </tbody> 23</table> 24
configurations_controller.rb
1class Users::ConfigurationsController < ApplicationController 2before_action :set_user, only: [:show, :edit, :update, :destroy] 3 def index 4 @user = User.find_by_id(params[:user_id]) 5 end 6 7 def edit 8 end 9 10 def new 11 @user = User.new 12 end 13 14 def show 15 end 16 17 def destroy 18 @user.destroy 19 respond_to do |format| 20 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 21 format.json { head :no_content } 22 end 23 end 24 25end 26
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。