首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoMethodError in Stories#index,未定义的方法“每一个”

NoMethodError in Stories#index,未定义的方法“每一个”
EN

Stack Overflow用户
提问于 2016-11-28 19:34:02
回答 1查看 450关注 0票数 0

我看过其他帖子,但不明白问题出在哪里。我的表演方法有问题吗?我在听一篇教程,他把它留了下来。

Index.html.erb

代码语言:javascript
复制
<% 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 %>

我的故事控制器:

代码语言:javascript
复制
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

结束

错误:

代码语言:javascript
复制
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: 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-28 19:41:37

代码语言:javascript
复制
class StoriesController < ApplicationController
    before_action :find_story, only: [:destroy, :show, :edit, :update]
end  # <--- this `end` breaks everything 

您很快就结束了控制器定义。因此,它没有定义任何操作方法(这意味着没有@stories )。

所有这些方法都应该把放到控制器中,而不是坐在外面。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40852086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档