帖子#index中的NoMethodError

时间:2014-01-23 06:50:30

标签: ruby-on-rails

嗨,我对铁轨上的红宝石很新。我遵循用户指南。我有一些错误。我试图搜索这个,但我发现了一个。问题是我们都有相同的错误,但当我试图改变@posts = Post.all我仍然有一些错误,有人可以帮助我吗?这是我的代码如下。 这是我的post_controller.rb

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def new

  end

  def create
    #render text: params[:post].inspect
    @post = Post.new(post_params)

    @post.save
    redirect_to @post
  end

  def show
    @post = Post.find(params[:id])
  end

  private
    def post_params
      params.require(:post).permit(:title, :text)
    end

end

我的index.html.erb

<h1>Listings!</h1>
  <table>
    <tr>
      <th>Title</th>
      <th>Text</th>
    </tr>
    <tr>
      <% @posts.each do |posts| %>
      <tr>
        <td><%= @posts.title %></td>
        <td><%= @posts.text %></td>
      </tr>
      <% end %>
    </tr>
  </table>

当我尝试访问localhost时 得到了这个错误 帖子#index

中的NoMethodError

1 个答案:

答案 0 :(得分:1)

转换此

 <% @posts.each do |posts| %>
  <tr>
    <td><%= @posts.title %></td>
    <td><%= @posts.text %></td>
  </tr>
  <% end %>

到这个

 <% @posts.each do |posts| %>
  <tr>
    <td><%= posts.title %></td>
    <td><%= posts.text %></td>
  </tr>
  <% end %>
相关问题