Skip to content

Instantly share code, notes, and snippets.

@dmitryame
Created March 30, 2011 12:42
Show Gist options
  • Save dmitryame/894318 to your computer and use it in GitHub Desktop.
Save dmitryame/894318 to your computer and use it in GitHub Desktop.
A presentation script to demo how easy it is to create a blog app in 10 minutes with rails
rvm install 1.9.2
rvm use 1.9.2
gem list
gem install rails
gem list
rails new blog
cd blog
bundle
rails g scaffold post title:string text:text
rails g model comment post_id:integer text:text
rake db:migrate
rails s
-- blowser
http://localhost:3000
create a post
http://localhost:3000/posts
new post
myfirst post
my first text
-- mate
-- app/views/posts/index.html.erb
<h1>Welocome to my BlogPost</h1>
<% @posts.each do |post|%>
<h2><%= link_to post.title, post %> </h2>
<p>
<%= time_ago_in_words post.created_at %> ago
</p>
<p>
<%= truncate post.text %>
</p>
<%end%>
<p>
<%= link_to "New Post", new_post_path %>
</p>
-- app/views/posts/show.html.erb
<h1><%= @post.title %> </h1>
<%= @post.text %>
<p>
<%= link_to "Back", posts_path %>
|
<%= link_to "Edit", edit_post_path(@post) %>
|
<%= link_to "Delete", @post, :method => :delete, :confirm => "Are you sure?" %>
</p>
-- adding comments
-- app/views/posts/show.html.erb
<h1><%= @post.title %> </h1>
<%= @post.text %>
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p><%=comment.text%></p>
<p><%=time_ago_in_words comment.created_at %> ago </p>
<%end%>
<%= form_for [@post, @post.comments.build] do |f| %>
<p><%= f.text_area :text, :size => '40x10' %></p>
<p><%= f.submit "Post Comment" %></p>
<%end%>
<p>
<%= link_to "Back", posts_path %>
|
<%= link_to "Edit", edit_post_path(@post) %>
|
<%= link_to "Delete", @post, :method => :delete, :confirm => "Are you sure?" %>
</p>
--updating model
--app/models/post.rb
has_many :comments
--app/models/comment.rb
belongs_to :post
--routes.rb
resources :posts do
resources :comments
end
rails g controller comments create destroy
--app/controller/comments_controller.rb
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
@comment.save
redirect_to @post
end
-- app/views/posts/show.html.erb
<p><%= link_to "Delete comment", [@post, comment], :method => :delete , :confirm => "Are you sure?" %></p>
--app/controller/comments_controller.rb
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @comment.post
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment