@inrmnn

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

rspec のエラー解決方法を知りたい

初学者です。
他人のユーザ編集画面へのリンクが存在すると、自分のユーザ編集画面へのリンクは存在しないがわからないです

発生している問題・エラー

 1) [STEP3] 仕上げのテスト 他人の画面のテスト 他人の投稿詳細画面のテスト サイドバーの確認 他人のユーザ編集画面へのリンクが存在する
     Failure/Error: expect(page).to have_link '', href: edit_user_path(other_user)
       expected to find link "" with href "/users/1/edit" but there were no matches
     # ./spec/system/03_finishing_touches_spec.rb:248:in `block (5 levels) in <main>'

  2) [STEP3] 仕上げのテスト 他人の画面のテスト 他人の投稿詳細画面のテスト サイドバーの確認 自分のユーザ編集画面へのリンクは存在しない
     Failure/Error: expect(page).not_to have_link '', href: edit_user_path(user)
       expected not to find visible link "" with href "/users/2/edit", found 1 match: ""
     # ./spec/system/03_finishing_touches_spec.rb:255:in `block (5 levels) in <main>'

books/show/html.erb

 <%= render 'books/errors', model: @book %>

<div class="row">
    <div class="col-md-3">
        
         <%= render 'users/user', user: @user %>
         
         <h2 class="mt-3">New book</h2>
         
          <%= render 'books/book', book: @newbook %>

    </div>
    <div class="col-md-8 offset-md-1">
        <h2>Book detail</h2>
        <table class="table">
              <tr>
                <td>
                    <%= image_tag @book.user.get_profile_image(100,100) %><br>
                    <%= link_to @user.name, user_path(@user) %>
                </td>
                <td class="book-title"><%= @book.title %></td>
                <td class="book-body"><%= @book.body %></td>
                <% if @book.user_id == current_user.id %>
                  <td><%= link_to "Edit", edit_book_path, class:"btn btn-success" %></td>
                  <td><%= link_to "Destroy", book_path, method: :delete, data: {confirm: "本当に消しますか?"}, class:"btn btn-danger" %></td>
                <% end %>
              </tr>
        </table>
    </div>
</div>

books/controller

class BooksController < ApplicationController
  
  def index
    @books = Book.all
    @book = Book.new
    @user = current_user
    
  end
  
  def edit
    @book = Book.find(params[:id])
    if @book.user == current_user
      render "edit"
    else
      redirect_to books_path
    end
    
  end 
  
  def create
    @book = Book.new(book_params)
    @book.user_id = current_user.id
    if  @book.save
        redirect_to book_path(@book), notice: "You have created book successfully."
    else
         @user = current_user
         @books = Book.all
         render "index"
    end 
   
  end
  
  def update
    @book = Book.find(params[:id])
    @book.user_id = current_user.id
    if  @book.update(book_params)
        redirect_to book_path(@book), notice: "Book was successfully update."
    else
        render "edit"
    end 
  end 
  
  def destroy
    @book = Book.find(params[:id])
    @book.destroy
    redirect_to books_path
  end 

  def show
    @book = Book.find(params[:id])
    @user = @book.user
    @newbook = Book.new
  end
  
  private
  
  def book_params
    params.require(:book).permit(:title, :body)
  end 
end

user/_user.html.erb

<h2>User info</h2>
            <%= image_tag user.get_profile_image(100,100) %>
            <table class="table">
                <tbody>
                    <tr></tr>
                    <tr>
                        <th>name</th>
                        <th><%= user.name %></th>
                    </tr>
                    <tr>
                        <th>introduction</th>
                        <th><%= user.introduction %></th>
                    </tr>
                </tbody>
            </table>
            <div class="row">
               
                <% if @user == current_user %>
                    <%= link_to edit_user_path(current_user), class: "btn btn-outline-secondary btn-block" do %>
                      <i class="fas fa-user-cog"></i>
                    <% end %>
                <% else %>
                    <%= link_to edit_user_path, class: "btn btn-outline-secondary btn-block" do %>
                      <i class="fas fa-user-cog"></i>
                    <% end %>
                <% end %>
                    
               
            </div>

お願いします。
足りないコードがあったら教えていただけると幸いです

0 likes

2Answer

<% if @user == current_user %>
<%= link_to edit_user_path(current_user), class: "btn btn-outline-secondary btn-block" do %>

<% end %>
<% else %>
<%= link_to edit_user_path, class: "btn btn-outline-secondary btn-block" do %>

<% end %>
<% end %>

もしかしたら、上記の2行目のlink_to のあとのedit_user_pathが間違っているかもしれません。

コマンドで
rails routes |grep user
で確認してみてください。
もしかしたら。
config/routes.rbにルートの記述がない可能性があります。

0Like

Comments

  1. 上記のif文が原因だと思いました。

Your answer might help someone💌