Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: xmonkey/railstutors_project
base: fbc7acfc5a
...
head fork: xmonkey/railstutors_project
compare: 76a2d5897b
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
11 app/controllers/comments_controller.rb
@@ -0,0 +1,11 @@
+class CommentsController < ApplicationController
+ def create
+ @post = Post.find(params[:post_id])
+ @comment = @post.comments.create(params[:comment])
+ if @comment.save
+ redirect_to @post
+ else
+ redirect_to @post, notice: 'Failed to add new comment.'
+ end
+ end
+end
View
14 app/controllers/posts_controller.rb
@@ -2,7 +2,21 @@ class PostsController < ApplicationController
def index
@posts = Post.all
end
+
def show
@post = Post.find(params[:id])
end
+
+ def new
+ @post = Post.new
+ end
+
+ def create
+ @post = Post.create(params[:post])
+ if @post.save
+ redirect_to posts_path
+ else
+ redirect_to @post, notice: 'Failed to add new post.'
+ end
+ end
end
View
4 app/views/posts/index.html.erb
@@ -1,5 +1,7 @@
<h2>Post list</h2>
<% @posts.each do |post| %>
<p> <%= link_to post.title, post.url %>
- <%= pluralize(post.comments.count, ' comment') %> </p>
+ <%= link_to pluralize(post.comments.count, ' comment'), post %> </p>
<% end %>
+
+<%= link_to "Submit a Link", new_post_path %>
View
14 app/views/posts/new.html.erb
@@ -0,0 +1,14 @@
+<h2>Add new Post</h2>
+<%= form_for (@post) do |f| %>
+ <div class="field">
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </div>
+ <div class="field">
+ <%= f.label :url %><br />
+ <%= f.text_field :url %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
4 app/views/posts/show.html.erb
@@ -5,3 +5,7 @@
<li> <%= comment.content %> </li>
<% end %>
</ul>
+<%= form_for @post.comments.new, :url => post_comments_path(@post) do |f| %>
+ <%= f.text_area :content, :size => "60x2" %>
+ <%= f.submit "Add comment" %>
+<% end %>
View
4 config/routes.rb
@@ -1,5 +1,7 @@
CourseProject::Application.routes.draw do
#root to: "main#index"
root to: "posts#index"
- resources :posts, :only => [:index, :show]
+ resources :posts, :only => [:index, :show, :new, :create] do
+ resources :comments, :only => :create
+ end
end

No commit comments for this range

Something went wrong with that request. Please try again.