Skip to content

Commit f7d67b6

Browse files
committed
answers scaffold + creation feature
1 parent fba362e commit f7d67b6

34 files changed

Lines changed: 439 additions & 11 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Answers controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

app/assets/stylesheets/scaffolds.css.scss

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class AnswersController < InheritedController
2+
load_and_authorize_resource
3+
4+
belongs_to :question
5+
6+
def create
7+
@answer = Answer.create params[:answer]
8+
@answer.question = @question
9+
@answer.user = current_user
10+
create!(:notice => "Answer Posted!"){ question_url(params[:question_id]) }
11+
end
12+
13+
14+
end

app/controllers/questions_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ def create
66
@question.user = current_user
77
create!(:notice => "Question Asked!"){ collection_url }
88
end
9+
10+
def show
11+
@answer = Answer.new
12+
show!
13+
end
914
end

app/helpers/answers_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module AnswersHelper
2+
end

app/models/ability.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ class Ability
44
def initialize(user)
55
user ||= User.new
66

7-
can :read, Question
7+
can :read, :all
88

99
unless user.new_record?
10-
can :create, Question
11-
can :manage, Question, :user => user
10+
can :create, [Question, Answer]
11+
can :manage, [Question, Answer], :user => user
1212
end
1313

1414
# Define abilities for the passed in user here. For example:

app/models/answer.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Answer
2+
include MongoMapper::Document
3+
4+
key :description, String
5+
6+
validates_presence_of :description
7+
8+
belongs_to :question
9+
belongs_to :user
10+
11+
end

app/models/question.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Question
55
key :description, String
66

77
belongs_to :user
8+
many :answers
89

910
validates_presence_of :title, :description
1011

app/models/user.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ class User
88
key :username, String
99
key :email, String
1010

11+
many :questions
12+
many :answers
13+
1114
end

0 commit comments

Comments
 (0)