t56k
t56k

Reputation: 7011

Undefined methods using strings to populate inputs in Rails

I'm trying to pass a string from a variable in my Rails app to the database. It's the user's email address. I cannot for the life of me figure out why I'm getting an "undefined method `merge'" error.

Here's the offending code:

<%= f.email_field :commenter, current_user.email %>

Any help will surely help. Cheers!

Upvotes: 0

Views: 98

Answers (1)

sikachu
sikachu

Reputation: 1221

Rails expect the second argument to be a hash, so you'd want to do

<%= f.email_field :commenter, value: current_user.email %>

Or better yet, set the :commenter attribute in that particular model in the controller (after you initalize/build it), then you can leave out this value hax. That way, when use edit this, post the form, and get a validation error, the value will be what's user were posting, not current_user.email.

Upvotes: 1

Related Questions