j_e
j_e

Reputation: 57

form_for with non-model field

I am using Rails's form_for feature to create a form that builds an object. Besides the fields that provide the object's parameters, I want to include a few non-model fields that I can then access in the controller. When I use a non-model symbol for the field, f.check_box, Rails throws an undefined method error. Can anyone explain a better way to do this?

Upvotes: 1

Views: 1794

Answers (1)

oreoluwa
oreoluwa

Reputation: 5633

Yes, you would get the error because the attribute is not defined on the model.

2 ways I could think of in order to resolve this are:

  1. Instead of binding the f.check_box to the form, use check_box instead, whatever name you call it will be available within the controller.

  2. Define an attribute accessor within you model and use it with the f.check_box.

Upvotes: 3

Related Questions