Ruby on rails 故事中的命名错误#索引,未定义方法';每个';

Ruby on rails 故事中的命名错误#索引,未定义方法';每个';,ruby-on-rails,ruby,Ruby On Rails,Ruby,我看过其他帖子,但我不明白问题出在哪里。我的展示方式有问题吗?我正在学习一个教程,他却把它留白了 Index.html.erb <% page_header "Our cool stories" %> <p><%= link_to 'Tell one!', new_story_path, class: 'btn btn-primary btn-large' %></p> <% @stories.each do |stories| %>

我看过其他帖子,但我不明白问题出在哪里。我的展示方式有问题吗?我正在学习一个教程,他却把它留白了

Index.html.erb

<% page_header "Our cool stories" %>

<p><%= link_to 'Tell one!', new_story_path, class: 'btn btn-primary btn-large' %></p>

<% @stories.each do |stories| %>
  <div class="well well-lg">
    <h2><%= link_to story.title, story_path(story) %></h2>

    <p><%= truncate(story.body, length: 350) %></p>

    <div class="btn-group">
      <%= link_to 'Edit', edit_story_path(story), class: 'btn btn-info' %>
      <%= link_to 'Delete', story_path(story), data: {confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-danger' %>
    </div>
  </div>
<% end %>

我的故事控制器:

class StoriesController < ApplicationController
    before_action :find_story, only: [:destroy, :show, :edit, :update]
end

def index
    @stories = Story.order('created_at DESC')
end

def new
    @story = Story.new
end

def create
    @story = Story.new(story_params)
    if @story.save
        flash[:success] = "Your beautiful story has been added!"
        redirect_to root_path
    else
        render 'new'
    end
end

def edit
end

def update
    if @story.update.attributes(story_params)
        flash[:success] = "More knowledge, more wisdom"
        redirect_to root_path
    else
        render 'edit'
    end
end

def destroy
    if @story.destroy
        flash[:success] = "I think you should have more confidence in your storytelling"
    else
        flash[:error] = "Can't delete this story, sorry"
    end

def show
    @stories = Story.all
end

private

def story_params
    params.require(:story).permit(:title, :body)
end

def find_story
    @story = Story.find(params[:id])
end
class StoriesController
结束

错误:

NoMethodError in Stories#index

Showing /home/benjamin/Desktop/oxygen/app/views/stories/index.html.erb where line #5 raised:

undefined method `each' for nil:NilClass

Extracted source (around line #5):


    <p><%= link_to 'Tell one!', new_story_path, class: 'btn btn-primary btn-large' %></p>

    <% @stories.each do |stories| %>
      <div class="well well-lg">
        <h2><%= link_to story.title, story_path(story) %></h2>

I also receive this error in my terminal:

     ActionView::Template::Error (undefined method `each' for nil:NilClass):
        2: 
        3: <p><%= link_to 'Tell one!', new_story_path, class: 'btn btn-primary btn-large' %></p>
        4: 
        5: <% @stories.each do |stories| %>
        6:   <div class="well well-lg">
        7:     <h2><%= link_to story.title, story_path(story) %></h2>
        8: 
故事中的命名错误#索引 显示/home/benjamin/Desktop/oxygen/app/views/stories/index.html.erb,其中第5行出现: nil:NilClass的未定义方法“each” 提取的源(第5行附近):

我在终端中也收到此错误: ActionView::Template::Error(未定义nil:NilClass的“each”方法): 2: 3:

4: 5: 6: 7: 8:
类StoriesController类StoriesControllerend#在第5行中@stories为nil导致此错误的端点是什么?还有@stories.each do | stories |通常是:do | story |(删除复数化)发布错误的rails服务器日志。包括url、参数、异常和所有内容。这是
index
的模板还是
show
的模板?@whodini9:注意,他确实有
@stories=Story。show action第5行中的所有
都是零@stories是零导致此错误的端点是什么?还有@stories.each do | stories |通常是:do | story |(删除复数化)发布错误的rails服务器日志。包括url、参数、异常和所有内容。这是
index
的模板还是
show
的模板?@whodini9:注意,他确实有
@stories=Story.all
action@jan:从技术上讲,
end
不会破坏任何东西。:)@简:从技术上讲,这是一个“结束”的过程
class StoriesController < ApplicationController
    before_action :find_story, only: [:destroy, :show, :edit, :update]
end  # <--- this `end` breaks everything