0

I am trying to get my Posts controller to work and I keep getting these error messages:

undefined method 'each' for `#<Post:0x000000062820c0>`   

and : match ? attribute_missing(match, *args, &block) : super

I just included Attribute Methods but the error is still the same. I am posting my controller file below. Thanks for the help.

class PostsController < ApplicationController
include ActiveModel::AttributeMethods  #Just added this..the error message was the    

#same without it.


def new
 @post = Post.new
end

def create
@post = Post.new(post_params)

if @post.save
  redirect_to @post #.find(params[:id])
else
  render :new
end
end

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

private

def post_params
params.require(:Title).permit(:Body, :url) 
end

end

Here is the show view:

<h1>Posts</h1> 

<% @post.each do |post| %> 
Title: <%= post.Title %><br>
Body: <%= post.Body %><br>
url: <%= post.url %><br>
<% end %>
tomcruise
  • 17
  • 1
  • 7

1 Answers1

0

SHOW action: Delete this line @post.each do |post| and also change these lines:

Title: <%= post.Title %><br> Body: <%= post.Body %><br>

to

Title: <%= @post.title %><br> Body: <%= @post.body %><br>

INDEX action: Instead of @post.each do |post| do

<% @posts.each do |post| %> Title: <%= post.title %><br> Body: <%= post.body %><br>

To be able to use it you would also want to add index action:

def index
  @posts = Post.all
end

Recommendation: Your post_params method is not written right. Instead do:

def post_params
  params.require(:post).permit(:title, :body, :url) 
end

What this method does is whitelisting permitted to massassignment parameters. Check this for more info.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • I did this and the original error message is gone and replaced by this "undefined method `each' for nil:NilClass" The error is in the @post.each do |post| line – tomcruise Aug 20 '14 at 14:05
  • @posts.each do |post| goes in the index view?? I am still getting the same error message with or without it there. – tomcruise Aug 20 '14 at 14:14
  • Okay now I get this error: undefined method `each' for nil:NilClass And it is coming from <% @posts.each do |post| %> in the show view. – tomcruise Aug 20 '14 at 14:22
  • This is what my index view is right now : <% @posts.each do |post| %> and yes I added the index action. Could it have something to do with some instance variables being post and some being posts, I just played around with that and it didn't fix anything – tomcruise Aug 20 '14 at 14:30
  • I did it and now its back to the original error messages: match ? attribute_missing(match, *args, &block) : super and: undefined method `each' for # – tomcruise Aug 20 '14 at 14:36
  • i beg you to check every letter. It matters. Read CAREFULLY what is in my answer, please – Andrey Deineko Aug 20 '14 at 14:39
  • should my instance variables be @post or @posts? In the index action it is posts and all my other actions are just post. – tomcruise Aug 20 '14 at 14:43
  • I forgot to delete the first line out of my show view. Now I'm getting this error: syntax error, unexpected keyword_ensure, expecting end-of-input and the problem seems to have something to do with this : actionview (4.1.4) lib/action_view/template.rb – tomcruise Aug 20 '14 at 14:49
  • so check the syntax carefully (, < %> {} signs whatsoever). and please accept the answer as the issue from the question is solved. – Andrey Deineko Aug 20 '14 at 14:50
  • Thank you. I have another question if you are interested: http://stackoverflow.com/questions/25376395/i-am-trying-to-redirect-to-the-show-actionso-i-can-see-the-new-post-if-a-save – tomcruise Aug 20 '14 at 15:15