diff --git a/AskBeaver/app/controllers/comments_controller.rb b/AskBeaver/app/controllers/comments_controller.rb index 7669955..748ac7e 100644 --- a/AskBeaver/app/controllers/comments_controller.rb +++ b/AskBeaver/app/controllers/comments_controller.rb @@ -1,2 +1,24 @@ class CommentsController < ApplicationController + def index + @question_answer = find_question_answer + @comments = @question_answer.comments + end + + def create + @question_answer = find_question_answer + @comment = @question_answer.comments.create(params[:comment]) + redirect_to polymorphic_path[@question_answer, Comment] + end + + private + + def find_question_answer + params.each do |name, value| + if name =~ /(.+)_id$/ + return $1.classify.constantize.find(value) + end + end + nil + end + end diff --git a/AskBeaver/app/controllers/questions_controller.rb b/AskBeaver/app/controllers/questions_controller.rb index 77f77ca..10ea146 100644 --- a/AskBeaver/app/controllers/questions_controller.rb +++ b/AskBeaver/app/controllers/questions_controller.rb @@ -14,7 +14,6 @@ def index # GET /questions/1.json def show @question = Question.find(params[:id]) - respond_to do |format| format.html # show.html.erb format.json { render json: @question } diff --git a/AskBeaver/app/models/answer.rb b/AskBeaver/app/models/answer.rb index f274b7a..fbdf9a3 100644 --- a/AskBeaver/app/models/answer.rb +++ b/AskBeaver/app/models/answer.rb @@ -1,6 +1,6 @@ class Answer < ActiveRecord::Base belongs_to :question attr_accessible :content - has_many :comments, :as => :question_answer, :dependent => :destroy + has_many :comments, :as => :question_answer end diff --git a/AskBeaver/app/models/question.rb b/AskBeaver/app/models/question.rb index f2cfae1..8f3e1c1 100644 --- a/AskBeaver/app/models/question.rb +++ b/AskBeaver/app/models/question.rb @@ -1,5 +1,5 @@ class Question < ActiveRecord::Base attr_accessible :content, :title - has_many :answers, :dependent => :destroy - has_many :comments, :as => :question_answer, :dependent => :destroy + has_many :answers + has_many :comments, :as => :question_answer end diff --git a/AskBeaver/app/views/questions/show.html.erb b/AskBeaver/app/views/questions/show.html.erb index d901318..af1a3d2 100644 --- a/AskBeaver/app/views/questions/show.html.erb +++ b/AskBeaver/app/views/questions/show.html.erb @@ -10,21 +10,50 @@ <%= @question.content %>

+ +

+ + +

Comments

+<% @question.comments.each do |comment| %> +

+ Comment: + <%= comment.content %> +

+<% end %> + + + + +

Add a comment:

+<%= form_for([@question_answer, Comment.new]) do |f| %> +
+ <%= f.label :content %>
+ <%= f.text_field :content %> +
+
+ <%= f.submit %> +
+<% end %> + + +

Answers

<% @question.answers.each do |answer| %>

Answer: <%= answer.content %> -

- +

<%= link_to 'Destroy Answer', [answer.question, answer], :confirm => 'Are you sure?', :method => :delete %>

+ + <% end %> -

Add a answer:

+

Add a answer:

<%= form_for([@question, @question.answers.build]) do |f| %>
<%= f.label :content %>
@@ -37,4 +66,4 @@ <%= link_to 'Edit', edit_question_path(@question) %> | -<%= link_to 'Back', questions_path %> +<%= link_to 'Back', questions_path %> \ No newline at end of file