-
Notifications
You must be signed in to change notification settings - Fork 52
Usage in Nested Model Form
akonan edited this page Sep 28, 2010
·
2 revisions
-
In your Comment model, include Humanizer and add the #require_human_on method:
class Comment < ActiveRecord::Base belongs_to :post include Humanizer require_human_on :create end
-
In your Post model, define the relationship:
class Post < ActiveRecord::Base has_many :comments accepts_nested_attributes_for :comments end
-
In your posts_controller.rb file, add a new comment under the 'show' action:
def show . . . @comment = Comment.new
-
In your comments_controller.rb file, for the 'create' action, edit your @comment assignment, and add a @post assignment:
def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment])
-
Ask the question in the form (/views/posts/show.html.erb for example):
<%= form_for [@post, @comment] do |f| %> . . . <%= f.label :humanizer_answer, @comment.humanizer_question %> <%= f.text_field :humanizer_answer %> <%= f.hidden_field :humanizer_question_id %>
Thanks to foamandthunder for creating this!