簡體   English   中英

如何解決此錯誤,我收到“抱歉,找不到您要查找的頁面。”

[英]How do i fix this error i'm getting “Sorry, the page you are looking for could not be found.”

我有這個應用程序,我試圖讓問題的作者能夠將答案標記為最佳答案。

我創建了一個AcceptAnswerController ,它已在routes/web.php文件中注冊,如下所示:

AcceptAnswerController.php

class AcceptAnswerController extends Controller
{
    public function __invoke(Answer $answer) 
    {
        $this->authorize('accept', $answer);

        $answer->question->acceptBestAnswer($answer);
        return back();
    }
}

web.php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('questions', 'QuestionsController')->except('show');

Route::resource('questions.answers', 'AnswersController')->only(['store', 'edit', 'update', 'destroy']);

Route::get('questions/{slug}', 'QuestionsController@show')->name('questions.show');

Route::post('answers/{answer}/accept', 'AcceptAnswerController')->name('answers.accept');

在我的答案視圖中,我有以下內容:

<div class="row mt-4">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">
                <div class="card-title">
                    <h2>{{ $answersCount . " " . str_plural('Answer', $answersCount) }}</h2>
                </div>
                <hr>
                @include ('layouts._messages')

                @foreach ($answers as $answer)
                    <div class="media">
                        <div class="d-fex flex-column vote-controls">
                            <a title="This answer is useful" class="vote-up">
                                <i class="fas fa-caret-up fa-3x"></i>
                            </a>
                            <span class="votes-count">1230</span>
                            <a title="This answer is not useful" class="vote-down off">
                                <i class="fas fa-caret-down fa-3x"></i>
                            </a>
                            @can ('accept', $answer)
                                <a title="Mark this answer as best answer" 
                                    class="{{ $answer->status }} mt-2"
                                    onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
                                    >
                                    <i class="fas fa-check fa-2x"></i>                                    
                                </a>
                                <form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
                                    @csrf
                                </form>
                            @else
                                @if ($answer->is_best)
                                    <a title="The question owner accepted this answer as best answer" 
                                        class="{{ $answer->status }} mt-2"                                        
                                        >
                                        <i class="fas fa-check fa-2x"></i>                                    
                                    </a>
                                @endif
                            @endcan
                        </div>
                        <div class="media-body">
                            {!! $answer->body_html !!}
                            <div class="row">
                                <div class="col-4">
                                    <div class="ml-auto">
                                        @can ('update', $answer)
                                            <a href="{{ route('questions.answers.edit', ['question' => $question->id, 'answer' => $answer->id]) }}" class="btn btn-sm btn-outline-info">Edit</a>
                                        @endcan
                                        @can ('delete', $answer)
                                            <form class="form-delete" method="post" action="{{ route('questions.answers.destroy', [$question->id, $answer->id]) }}">
                                                @method('DELETE')
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                            </form>
                                        @endcan
                                    </div>
                                </div>
                                <div class="col-4"></div>
                                <div class="col-4">
                                    <span class="text-muted">Answered {{ $answer->created_date }}</span>
                                    <div class="media mt-2">
                                        <a href="{{ $answer->user->url }}" class="pr-2">
                                            <img src="{{ $answer->user->avatar }}">
                                        </a>
                                        <div class="media-body mt-1">
                                            <a href="{{ $answer->user->url }}">{{ $answer->user->name }}</a>
                                        </div>
                                    </div>
                                </div>
                            </div>                            
                        </div>
                    </div>
                    <hr>
                @endforeach
            </div>
        </div>
    </div>
</div>

一旦問題的作者單擊復選圖標,它應該將答案標記為最佳答案,但我收到以下錯誤:

抱歉,找不到您要查找的頁面

我看到 url 中缺少答案 ID,如下所示:

http://localhost:8000/answers//接受

當它實際上應該是這樣的:

http://localhost:8000/answers/1/accept

我似乎無法弄清楚為什么當我將它作為表單操作中的路由參數傳遞時。 如果用戶試圖編輯他的答案,也會發生同樣的事情。

我可以建議在這里稍微調整一下您的策略並繞過提交表單的需要嗎?

如果您將 POST 換成 GET 請求,這實際上非常簡單。

Answers.blade.php

@foreach( $answers as $answer )
    @can('accept', $answer)
        <a href="answers/{{ $answer->id }}/accept" title="Mark this answer as best answer" class="{{ $answer->status }} mt-2">
            <i class="fas fa-check fa-2x"></i>
        </a>
    @else
        <p>Do your thing</p>
    @endcan 
@endforeach

路線.web.php

Route::get('answers/{answer}/accept', 'AcceptAnswerController');

因此,在仔細檢查了我的代碼之后,controller、路線、視圖等一切似乎都還不錯。 我能夠將問題隔離到該區域

@can ('accept', $answer)
 <a title="Mark this answer as best answer" 
   class="{{ $answer->status }} mt-2"
  onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
                                   >
    <i class="fas fa-check fa-2x"></i>                                    
 </a>
 <form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
     @csrf
 </form>
@endcan

我確信$answer->id應該返回正確的 id,但我不太確定$answer->status所以我決定檢查它在 Answer model 中定義的訪問器。

public function getStatusAttribute()
{
   return $this->isBest() ? 'vote-accepted' : '';
}

public function isBest()
{
   return $this->id = $this->question->best_answer_id; /** here is the problem **/
}

那里的問題正盯着我看。 上面的 isBest 方法應該返回一個 boolean 值,但我錯誤地分配了。 這是簡單的解決方法。

public function isBest()
{
   return $this->id === $this->question->best_answer_id; /** here is the problem **/
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM