Getting undefined method `each' for #<Post:0xb5d0df30> at index view - ruby-on-rails

Trying out ruby, i just asked a question but here's another, getting undefined method `each' for # and i tried a lot of stuff
<h1>This is the index page</h1>
<p>I need to get the new action running!</p>
<%= link_to 'My Blog', controller: 'posts' %>
</br>
<%= link_to 'New post', new_post_path %>
<h1>Listing posts</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% #posts.each do |post| %> ///<----ERROR HERE!
<tr>
<td><%= posts.title %></td>
<td><%= posts.description %></td>
<td><%= link_to 'Show', posts_path(posts) %></td>
<td><%= link_to 'Edit', edit_posts_path(posts) %></td>
</tr>
<% end %>
</table>
This is my controller:
class PostsController < ApplicationController
def index
#posts = Post.new
end
def edit
#posts = Post.find(params[:id])
end
def update
#posts = Post.find(params[:id])
if #posts.update(posts_params)
redirect_to #posts
else
render 'edit'
end
end
def new
#posts = Post.new
end
def create
#posts = Post.new(posts_params)
if #posts.save
redirect_to #post
else
render 'new'
end
end
def show
#posts = Post.find(params[:id])
end
private
def posts_params
params.require(:posts).permit(:title, :description)
end
end
I'm pretty sure i changed eveything to be matching like #posts from #post, i would appreciate if someone could help me again, i've tried looking for an answer but not getting luck, thank you

In your controller under the index do:
def index
#posts = Post.all
end
Then in your view after your each do refer to the variable as post, not posts.
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.description %></td>
<td><%= link_to 'Show', post_path(post) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
</tr>
<% end %>

Modify the index method in your controller to the following:
def index
#posts = Post.all
end
Post.new returns a single (empty) objet, the each method expects an enumerable (array, hash, collection,...), hence the error.

Your #posts instance variable is pointing to a new post record instead of a collection of posts. You need an array to iterate over instead of a single post.

If you want to call .each on an object, that object has to be made up of a collection of data (even if the "collection" has one element).
You're calling Post.new - meaning the .each method is going to return an exception that it cannot cycle through the expected collection.
You need to do the following:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
#posts = Post.all
end
end
This should get your code working.

Related

Error searching for :all

I am currently trying to create a search method. I have a database all setup; however, I am running into the errors:
ActiveRecord::RecordNotFound in ArticlesController#index
and:
Couldn't find Article with 'id'=all
Here is the pertinent code:
Articles_controller.rb
class ArticlesController < ApplicationController
def index
#articles = Article.all
#articles = Article.search(params[:id])
end
def show
#article = Article.find(params[:search])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(params.require(:article).permit(:title, :text))
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
article.rb
class Article < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
def self.search(search)
if search
#article = Article.find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
#article = Article.find(:all)
end
end
end
index.rb
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= form_tag articles_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</table>
Sorry for all the code to look through. The errors I am getting are from running localhost:3000/articles, where I receive these error messages from the server. I should note that I am still very new to both Ruby and Ruby on Rails; however, I aim to learn and find seeing proper code helps me quite significantly (I am dyslexic and tend to be a visual learner).
I truly appreciate your help, thanks in advance.
I think find can not take :all. the documentation says
"Using the find method, you can retrieve the object corresponding to the specified primary key that matches any supplied options. I think this is enough
Article.where('name LIKE ?', "%#{search}%")
or if you find all the articles
Article.all
Why do you have #articles = Article.search(params[:id]) in the index method?
Also, the stack trace will tell you exactly on which line the error occurs

Best in place gem not updating values - rails 4

so i have installed best in place gem in a rails 4 environment and initialised it correctly. (i can click on the name field and the box becomes editable).
I've this code in my admin_namespaced user controller
class Admin::UsersController < Admin::BaseController
def index
#users = User.all
# #columns = User.column_names
end
def show
#user = User.find(params[:id])
end
def update
#user = User.find params[:id]
respond_to do |format|
if #user.update_attributes(user_params)
format.html { redirect_to(user, :notice => 'User was successfully updated.') }
format.json { respond_with_bip(#user) }
else
format.html { render :action => "index" }
format.json { respond_with_bip(#user) }
end
end
end
private
def user_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
end
and basically i want to use it in conjuction with rails datatables gem that i successfully setup, to inline-edit corresponding fields.
this is the html.erb code in my user index view
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= link_to "Back", admin_path %>
<table class="display responsive no-wrap text-center" id="usertableadmin">
<thead>
<tr>
<th>id</th>
<th>Username</th>
<th>Email</th>
<th>Activated?</th>
<th>Admin?</th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= best_in_place user, :name%></td>
<td><%= user.email %></td>
<td><%= user.activated %></td>
<td><%= user.admin %></td>
</tr>
<% end %>
</tbody>
</table>
here is what the html code looks like on the tag that has the best_in_place initialization.
<span data-bip-type="input" data-bip-attribute="name" data-bip-object="user" data-bip-original-content="Example User" data-bip-url="/users/1" data-bip-value="Example User" class="best_in_place" id="best_in_place_user_1_name">Example User</span>
I dont know for sure but for some reason the fields do not get updated. When i click to change the name it gets reverted to the previous one.
I dont know if its because i have a namespace, admin/users or its because its the index action and not the show action.
any insight is welcome.
I've found the solution,
it seems the error was the update url it was wrong because of the namespace.
What i had to do, was to include url parameter like below
<td><%= best_in_place user, :name, url: "users/#{user.id}" %></td>

Ruby getting started guide: uninitialized constant PostsController::Posts

I've done the ruby getting started guide a few times over and I always end up with the same result.
Guids
NameError in PostsController#index
uninitialized constant PostsController::Posts
Extracted source (around line #21):
19
20
21
22
23
24
def index
#posts = Posts.all
end
def edit
Rails.root: C:/RailsTesting/blog
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:21:in `index'
Request
Out of frustration/desperation, I copied all of the files from the actual code supplied by the tutorial with no avail, please help.
Here's my index
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", controller: "posts" %>
<%= link_to 'New post', new_post_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
</tr>
...
Here is my posts_controller
class PostsController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(params[:post].permit(:title, :text))
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post = Post.find(params[:id])
end
def index
#posts = Posts.all
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :text))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Your index method has the Post class written in plural. Change it to Post.all
Modify your index action as:
def index
#posts = Post.all
end
Here Post is the model from where you are get all posts.

NoMethodError in Posts#index, undefined method `each' for nil:NilClass

I'm new to rails and following http://guides.rubyonrails.org/getting_started.html. I'm currently on section 5.8 which should list all the posts in my blog at localhost:3000/posts, but am instead getting a message:
NoMethodError in Posts#index
Showing /Users/sw/Code/blog/app/views/posts/index.html.erb where line #9 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #9):
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
Here's my posts_controller.rb:
class PostsController < ApplicationController
def index
#post = Post.all
end
def new
end
def create
#post = Post.new(params[:post].permit(:title, :text))
#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 here's my index.html.erb:
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
</tr>
<% end %>
I've been searching all over and haven't been able to find an answer!
You are looping through a variable #posts, but in your index you are assigning a variable #post.
Thus, in your posts_controller.rb replace
def index
#post = Post.all
end
with
def index
#posts = Post.all
end

Rails kaminari pagination for child table

I have 2 classes: Posts and Comments, where posts has_many :comments and comments belongs_to post.
Each of my posts has a show page with a list of comments and I would like to paginate the comments. With the current code I have, I'm showing a list of all the comments on all the pages. So, If i have 10 comments and I want to have 2 on each page, I get 5 pages with the original 10 comments on it. Could someone shed some light?
My Code:
Posts controller:
def show
#post = Post.find(params[:id])
#comments = #post.comments.page(params[:page]).per(3)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
"Show" views:
<%= paginate #comments %>
<% #post.comments.each_with_index do |comments, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= comment.date %></td>
<td><%= comment.text %></td>
</tr>
<% end %>
</table>
You need to use the paginated object in the view, not get them fresh from the database:
<% #comments.each_with_index do |comments, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= comment.date %></td>
<td><%= comment.text %></td>
</tr>
<% end %>
This gets them fresh, unpaginated:
#post.comments

Resources