top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Rails: form.view.erb containing model related and non-model related fields?

+1 vote
343 views

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?

posted Jun 28, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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.

answer Jun 28, 2015 by Sheetal Chauhan
Similar Questions
+2 votes

I am working on building an api client for the Rakuten Market-place. I have got some test requests for add delete update etc working and thought I should aim to structure it as a Gem and publish it so that others can use it/enhance it.

I haven't built a Gem before (worked mostly within the rails environment to date).

Have been reading and looking at other api client gems and am making progress on building something (still got a way to go to handle errors etc.)

To make the gem general purpose though, I am trying to figure out the best way to provide the mapping between models in a rails app and the api client objects (such as product, category, order etc).

I am aiming to make each client api object a class be (or should i call them models?)
Then I suspect I will use new to build an api instance from a rails instance, and find to return an api instance to a rails instance.

I can build in mapping for my own models to the api objects with no problem, but I can't see how I could generalize this so that other apps with similar models could use the gem.

I suspect there are approaches for doing this, but so far I haven't managed to come up with how to do it. If anyone has any wisdom on the matter I would be grateful.

+2 votes

I am trying to write a simple calendar app and having troubles with time zones. For as many nice Date and Time helpers, action view and active record sure dont play nice with time zones.

I have an appointment model with the appointment_time attribute which is a datetime. My current issue is editing appointment_time with datetime_select within a form tag.

def appt_time
appointment_time.in_time_zone(time_zone) #this is the appointment owners time zone 
end

This gives a select with the current date and time in UTC, not the appointment date and time. appt_time does get called and returns the correct date and time adjusted for the time zone.

The docs mention nothing about time zones, what is the best way to accomplish this?

+1 vote

I have just started with rails. I am making a attendance web app. I want to insert student attendance into attendances table and unable to do it as the form just inserts only last entry of the form. Student table and Attendances table have associations (has_many,belongs_to).

Please let me know how the form should be and controller API should look like.

...