簡體   English   中英

Rails - 為什么我為 nil:NilClass 得到未定義的方法“名稱”

[英]Rails - why I'm getting undefined method `name' for nil:NilClass

我按類別顯示問題,每個問題都有自己的編號。 我是這樣制作的(index.html.erb):

<% @categories.each do |category| %>
     <div id=<%= "box#{category.id}" -%>>
        <h1><%= category.name %></h1>
        <% category.questions.each_with_index do |question, i| %>
            <ul class="question-list" style="display: inline;">
                <li><%= link_to (i + 1), show_path %></li>
            </ul>
        <% end %>
     </div>
<% end %>

當我點擊問題編號時,我想像這樣顯示它(show.html.erb):

<div class="question-wrapper">
       <h1><%= @category.name %></h1>
       <div class="image left-align">
         <%= question_image_tag(question, :large) %>
       </div>
       <div class="question-text right-align">
        <%= @question.question_name %>
       </div>
       <p><%= @question.answer %></p>
 </div>

這是我的控制器:

def index
    @categories = Category.all
    @questions = Question.all
end

def show
    @categories = Category.all
    @questions = Question.all
end

現在,我得到了 nil:NilClass 的未定義方法`name'(也為 show.html.erb 中的所有方法拋出錯誤),我無法解決它。

問題:我應該怎么做才能解決 nil:NilClass 的未定義方法 `name'?

更新: - 堆棧跟蹤

NoMethodError - 未定義的方法category' for nil:NilClass: app/controllers/quizz_controller.rb:13:in show'

路由文件

Rails.application.routes.draw do
    mount Attachinary::Engine => "/attachinary"
    root to: "quizz#home"
    %w(quizz
       show).each do |page|
       get page, to: "quizz##{page}", as: page
    end
    resources :categories
    resources questions
end

你在和代碼開玩笑。

def index
    @categories = Category.all
    @questions = Question.all
end

def show
    @category = Category.find(id-of-category)
    @question = Question.find(id-of-question)
end

當您調用 index 方法時,這意味着您需要向用戶顯示所有問題和類別。

但是當您需要顯示特定問題或顯示方法時,這意味着您需要找到該特定問題和類別。

在您的 show.html.erb 中,您使用了 @category 和 @question 之類的實例變量,您能告訴我您在哪里定義了要在視圖上使用的相同變量嗎?

@Unknown19 用戶,我想建議您這樣認為,您剛剛閱讀了 Scaffold 並嘗試使用 Scaffold 實現單個模型我相信您會學到很多東西以及您應該如何編寫方法及其實例,這是您學習 Restful 機制的最佳方式。

你只需要傳遞的問題IDPARAMS和找到在您的控制器的show行為問題的id @question。

您各自的視圖和控制器代碼應如下所示:

index.html.erb

<% @categories.each do |category| %>
 <div id=<%= "box#{category.id}" -%>>
    <h1><%= category.name %></h1>
    <% category.questions.each_with_index do |question, i| %>
        <ul class="question-list" style="display: inline;">
            <li><%= link_to (i + 1), show_path(question_id: question.id) %></li>
        </ul>
    <% end %>
 </div>

控制器

def show
  @question = Question.find_by_id(params[:question_id])
  @category = @question.category
end

顯示.html.erb

<div class="question-wrapper">
   <h1><%= @category.name %></h1>
   <div class="image left-align">
     <%= question_image_tag(question, :large) %>
   </div>
   <div class="question-text right-align">
    <%= @question.question_name %>
   </div>
   <p><%= @question.answer %></p>
</div>

單擊問題編號時只需發送 category_id 和 question_id 並將顯示方法的代碼更改為

def show 
  @category = Category.find(params[:category_id])
  @question = Question.find(params[:question_id])
end

並更改 index.html.erb 形式

<li><%= link_to (i + 1), show_path %></li>

<li><%= link_to (i + 1), show_path(category_id: category.id, question_id: question.id) %></li>

在參數中傳遞類別 id 和問題 id,如下所示:

<li><%= link_to (i + 1), question(category_id: category.id, question_id: question.id) %></li>

在您的控制器中寫入以下內容:

def show 
  @category = Category.find(params[:category_id])
  @question = Question.find(params[:question_id])
end

暫無
暫無

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

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