Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created March 26, 2011 04:33
Show Gist options
  • Save kracekumar/888032 to your computer and use it in GitHub Desktop.
Save kracekumar/888032 to your computer and use it in GitHub Desktop.
#post_controller.rb
class PostController < ApplicationController
filter_access_to :all
before_filter :login_required
def add
@posts=Post.new(params[:posts])
@posts.author = current_user
if request.post? and @posts.save
flash[:notice]='Post Published'
end
def all
@post=Post.find(:all)
end
end
end
#all.rhtml
<div id="content-header">
<img src="/images/manage_news/manage_news_logo.png" />
<h1>Forum / Blog / Q & A</h1>
<h3>Latest Knowledge Thirst</h3>
<div id="app-back-button">
<%= link_to_function image_tag("/images/buttons/back.png",:border => 0), "history.back()" %>
</div>
</div>
<div id="page-yield">
<% unless flash[:notice].nil? %>
<p class="flash-msg"> <%= flash[:notice] %> </p>
<% end %>
<div id="all_news">
<ul>
<% for posts in @post %>
<li><%= link_to posts.title, :controller=> 'post', :action=>'view',:id=>posts.id %>
<%end%>
</ul>
</div>
</div>
#error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
But i have checked in db i have one value and i also tried
kracekumar@kracekumar-laptop:/var/www/admin$ ruby script/console
Loading development environment (Rails 2.3.5)
/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/console.rb:45: warning: Insecure world writable dir /opt/ns in PATH, mode 040777
>> p=Post.find(:all)
=> [#<Post id: 1, author_id: 1, title: "test", content: "<p>Testing ,does this really work ?</p>", created_at: "2011-03-25 15:54:06", updated_at: "2011-03-25 15:54:06">]
#log
Processing PostController#all (for 127.0.0.1 at 2011-03-26 10:12:07) [GET]
Parameters: {"action"=>"all", "controller"=>"post"}
User Columns (0.9ms) SHOW FIELDS FROM `users`
User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
privileges_users Columns (0.6ms) SHOW FIELDS FROM `privileges_users`
Privilege Load (0.1ms) SELECT * FROM `privileges` INNER JOIN `privileges_users` ON `privileges`.id = `privileges_users`.privilege_id WHERE (`privileges_users`.user_id = 1 )
Rendering template within layouts/application
Rendering post/all
ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each) on line #19 of app/views/post/all.rhtml:
16: <div id="all_news">
17: <ul>
18: <% logger.debug "\n\n\t#{@post}\n\n" %>
19: <% for posts in @post %>
20: <li><%= link_to posts.title, :controller=> 'post', :action=>'view',:id=>posts.id %>
21: <%end%>
22: </ul>
app/views/post/all.rhtml:19:in `_run_rhtml_app47views47post47all46rhtml'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
Rendered rescues/_trace (41.5ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)
#add log detials
Processing PostController#add (for 127.0.0.1 at 2011-03-26 10:28:24) [POST]
Parameters: {"commit"=>"► Publish Post", "action"=>"add", "authenticity_token"=>"A+JmgkBKiCxrpqk1H0n6HZJp6PnwaZFJCPjOp9N0jG0=", "controller"=>"post", "posts"=>{"title"=>"view ", "content"=>"<p>It is not working</p>"}}
User Columns (0.9ms) SHOW FIELDS FROM `users`
User Load (0.2ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
Post Columns (0.7ms) SHOW FIELDS FROM `posts`
CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
SQL (0.1ms) BEGIN
Post Create (0.4ms) INSERT INTO `posts` (`created_at`, `title`, `updated_at`, `content`, `author_id`) VALUES('2011-03-26 04:58:24', 'view ', '2011-03-26 04:58:24', '<p>It is not working</p>', 1)
SQL (42.8ms) COMMIT
Rendering template within layouts/application
Rendering post/add
User Load (0.2ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
Reminder Load (0.1ms) SELECT * FROM `reminders` WHERE (recipient = '1')
Rendered layouts/_reminder_count (5.1ms)
Completed in 112ms (View: 37, DB: 50) | 200 OK [http://localhost/post/add]
#db/migration script for this
class Post < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.references :author
t.string :title
t.text :content
t.timestamps
end
create_table :post_comments do |t|
t.references :author
t.text :comment
t.references :posts
t.timestamps
end
create_table :post_tags do |t|
t.references :posts
t.references :tags
t.timestamps
end
create_table :post_views do |t|
t.references :posts
t.integer :views
t.timestamps
end
create_table :post_ratings do |t|
t.references :posts
t.references :author
t.integer :ratings
t.timestamps
end
create_table :post_likes do |t|
t.references :posts
t.references :author
t.integer :likes
t.integer :dislikes
t.timestamps
end
end
def self.down
drop_table :posts
drop_table :post_comments
drop_table :post_tags
drop_table :post_views
drop_table :post_ratings
drop_table :post_likes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment