How can I make form.select return a model in Rails? -
I have a model that has been contacted, which is a common guest.
class contact & lt; ActiveRecord :: Base has_one: guest ,: class_name = & gt; "Contact",: foreign_key => "Guest" end In the make-up scene I am using select assist method to list all contacts that can be selected as a guest ... < / P>
& lt;% form_for (@contact) do | F | | & Gt%; & Lt;% = f.error_messages% & gt; ... & lt; P & gt; & Lt;% = f.label: Guest% & gt; & Lt; Br / & gt; & Lt;% @guests = Contact.find (: all, order = & gt; "last name"). Map {| U | [U.lastname + ',' + u.firstname, u.id]}%> & Lt;% = f.select: Guest, @guests, {: include_blank = & gt; True}%> & Lt; / P & gt; ... & lt;% end% & gt; Whenever this form is submitted I get an error by saying that it was expecting a contact but got the string.
Where and how am I in contact with a contact before saving the current contact to the database?
When you create an association in the rail, such as ___, there are certain methods for your object Dynamically generated for easy assignments.
Now, the method you use
@ contact.guest = @guest and expect your contact object To get another ActiveRecord object that may be associated with yourself, though what you are trying to do, there is a kind of "fake" method in your form that wraps the model object. (The information coming from the parameter is brought as a string, that's why you get this error)
There are several ways to implement this method. Along with using nested model features. . Complex forms that allow you to wrap a child union within form_for (using the fields_for form helper). It will take a long time to type an explanation about this, and fortunately there are incredible free resources on complex forms:
P> Also, if you are using Rail 2.3, then look for some information on "
accepts" _ . . . There is no separate shortage of tutorials here, but I imagine with the release of 2.3. There is a lot to come. This easy small assistant Ryan will cut the half-section used in his screen so that the form can accept a subboardet model. Finally there is an alternative solution if your relationship is simple, if you want the guest you are already in the database, and you do not need to create new ones from the form that you use If you are doing, you can use something like virtual feature. In your form, just change something like your f.select: guest to f.select: guest_select and apply a method to your model:
deaf guest_shackel = (incoming_ID_frame_form) contact = contact.find (incoming_id_from_form) self.guest = contact end which will provide an ActiveRecord model to implement a complex form you!
Comments
Post a Comment