Skip to content

Instantly share code, notes, and snippets.

@lenary
Forked from seivan/gist:289025
Created January 28, 2010 19:06
Show Gist options
  • Select an option

  • Save lenary/289026 to your computer and use it in GitHub Desktop.

Select an option

Save lenary/289026 to your computer and use it in GitHub Desktop.
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