However, I made a sample blogging application in my system. I am able to
insert a record in DB through console. But I am not been able to insert
data through my application, don’t know what I am doing wrong in that.
========================================================
post_contoller.rb file :
class PostsController < ApplicationController
def show
@post=Post.find(params[:id])
end
def new
@post=Post.new
end
def create
@post=Post.new(params[:id])
if @post.save
redirect_to posts_path, :notice => “your post has been inserted”
else
render “new”
end
end
def index
@post=Post.all
end
end
index.html.erb file
My blog
<% @post.each do |post| %>
<%= link_to post.title , post %>
<%= post.content %>
<% end %>
<%= link_to "Add new post", new_post_path %>
======================================================
new.html.erb file :
Add a new post
<%= form_for @post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit "Add a new post" %>
<% end %>
========================================================
Console Output:
Started POST “/posts” for 127.0.0.1 at 2015-04-07 23:05:52 +0530
Processing by PostsController#create as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“YjmybMkOsVRcHjOXvG1NrkFjuk9TGbu54Cd74mLfWJhOvkxBskWefnwAFq65EZmFxEqVSpbUL1OcvY9DHpVuiw==”,
“post”=>{“title”=>“Title1”, “content”=>“Content1”}, “commit”=>“Add a new
post”}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO “posts” (“created_at”, “updated_at”) VALUES
(?, ?) [[“created_at”, “2015-04-07 17:35:52.602692”], [“updated_at”,
“2015-04-07 17:35:52.602692”]]
(232.0ms) commit transaction
Redirected to http://localhost:3000/posts
Completed 302 Found in 237ms (ActiveRecord: 232.4ms)