form.view.erb containing model related and non-model related fields.

21 views
Skip to first unread message

Padmahas Bn

unread,
Jun 28, 2015, 2:36:34 AM6/28/15
to rubyonra...@googlegroups.com
Hello all,

I would like to have a form in which some of the fields are linked to model and some fields are not. When I use form_for tag it binds to a particular model and I cannot create form fields that aren't correspond to one of the column name of the table (Model). Hence what I think is I should use form_tag instead of form_for so that it contains whatever the form fields it want but one thing I can't find by googling is how can I connect specific fields to a model leaving other fields free of model?

In other words, If I have 5 text_fields in a form how can I link only 3 text_fields to a model and leave other 2 fields free?

Thank you.

Henry Lawson

unread,
Jun 28, 2015, 2:43:33 AM6/28/15
to rubyonra...@googlegroups.com
Hi,

Consider binding your form to an ActiveModel not an ActiveRecord class. In your ActiveModel, you can add in the extra fields that you don't want/have in your ActiveRecord class. Using ActiveModel will also let you follow the same idioms in ActiveRecord, such as validation.

Thanks,
Henry

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/056c9834-ebd0-4b28-8191-623c933d7c3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mobile: +61 4 0136 1909

Colin Law

unread,
Jun 28, 2015, 4:23:12 AM6/28/15
to rubyonra...@googlegroups.com
For the model fields populate the field with my_model.the_attribute
and for the others just use the value. Then extract the data from
params in the controller.

Or use ActiveModel as suggested by Henry, which is a more OO solution.

Colin

Frederick Cheung

unread,
Jun 28, 2015, 4:59:36 AM6/28/15
to rubyonra...@googlegroups.com, padm...@gmail.com
On Sunday, June 28, 2015 at 7:36:34 AM UTC+1, Padmahas Bn wrote:
> Hello all,
>
>
> I would like to have a form in which some of the fields are linked to model and some fields are not. When I use form_for tag it binds to a particular model and I cannot create form fields that aren't correspond to one of the column name of the table (Model). Hence what I think is I should use form_tag instead of form_for so that it contains whatever the form fields it want but one thing I can't find by googling is how can I connect specific fields to a model leaving other fields free of model?
>

You can still use form_for, but use text_field_tag rather than f.text_field for the non model form inputs.

Fred

Padmahas Bn

unread,
Jul 8, 2015, 8:10:14 PM7/8/15
to rubyonra...@googlegroups.com, padm...@gmail.com
You can still use form_for, but use text_field_tag rather than f.text_field for the non model form inputs.

Later how can I send those values inside text_field_tag to different controller's action using button_to?

thank you. 
Reply all
Reply to author
Forward
0 new messages