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
<h1>My blog</h1>
<% @post.each do |post| %>
<h2><%= link_to post.title , post %></h2>
<p><%= post.content %></p>
<% end %>
<p><%= link_to "Add new post", new_post_path %> </p>
======================================================
new.html.erb file :
<h1>Add a new post</h1>
<p><%= form_for @post do |f| %></p>
<p>
<%= f.label :title %> <br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %> <br/>
<%= f.text_area :content %>
</p>
<p>
<%= f.submit "Add a new post" %> <br/>
</p>
<% 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)
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/443f20012ceb14f6e98a44e6d8ef6c79%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.