Skip to content

Commit

Permalink
no senti que avanzo nada. problemas con comentarios polimorficos
Browse files Browse the repository at this point in the history
  • Loading branch information
warles34 committed May 21, 2013
1 parent 1e57015 commit e66e271
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
22 changes: 22 additions & 0 deletions AskBeaver/app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion AskBeaver/app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion AskBeaver/app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions AskBeaver/app/models/question.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 33 additions & 4 deletions AskBeaver/app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,50 @@
<%= @question.content %>
</p>

<!-- COMENTARIOS CORRESPONDIENTES A UNA PREGUNTA -->
<p>


<h2>Comments</h2>
<% @question.comments.each do |comment| %>
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<% end %>

<!-- CREACION DE UN NUEVO COMENTARIO -->


<h2>Add a comment:</h2>
<%= form_for([@question_answer, Comment.new]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

<!-- RESPUESTAS -->

<h2>Answers</h2>
<% @question.answers.each do |answer| %>
<p>
<b>Answer:</b>
<%= answer.content %>
</p>

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


<% end %>

<h2>Add a answer:</h2>
<h2>Add a answer:</h2>
<%= form_for([@question, @question.answers.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
Expand All @@ -37,4 +66,4 @@


<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>
<%= link_to 'Back', questions_path %>

0 comments on commit e66e271

Please sign in to comment.