-
-
Save lenary/289026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def new | |
| # supply the default values to the new form. | |
| # no params are getting posted to this so no need to look in params[] | |
| @comment = Comment.new(default_attributes) | |
| end | |
| def create | |
| # first step, make a new comment with the stuff posted to your action, but with the default attributes | |
| # | |
| # hash_a.merge(hash_b) gets every element of hash_b to override elements with the same key in hash_a, | |
| # but those only in hash_a or only in hash_b to stick around | |
| @comment = Comment.new(params[:form].merge(default_attributes)) | |
| if @comment.save | |
| # magic | |
| else | |
| # less magic | |
| end | |
| end | |
| private | |
| # always best to have it as a method. i'm not sure if a local variable would work there. | |
| # this does the same but has better scope | |
| def default_attributes | |
| {:name => cookies[:name], :email => cookies[:email]} | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment