billy crogan
billy crogan

Reputation: 9

NoMethodError in Posts#index

Hi im new to ruby on rails. And Im following the user guide. I got some errors on it. I tried to search this but i found one. The problem is we both have the same error, but when i tried to change @posts = Post.all i got still some errors, can someone help me on this? here's my code below. This is my 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

And my 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>

when i tried to access on localhost got this error NoMethodError in Posts#index

Upvotes: 0

Views: 880

Answers (1)

Jeet
Jeet

Reputation: 1380

convert this

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

to this

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

Upvotes: 1

Related Questions