With the gracious help of DHH, I now have a single action working that allows me to create multiple Members on an App, without any tedious "add another" submit-button clicking on the part of the user. Cool stuff. App model: has_many :members, :dependent => true Member model: belongs_to :app However, I''m still a little stuck with one particular facet of it. When the user submits the form, AR validates each Member and re-renders the page if there are errors. However, because the Members are children of the App model, they are automatically saved to the DB as they are attached. This means that, for example, 3 out of 6 members will be present in the DB, and the other 3, having errors, are not. When the user corrects the errors and re-submits the form, I need to make sure I don''t create duplicate Member records. How do I match these back up, since the "id" field is nowhere on the form, and half of the members don''t have ids yet? I can have the controller destroy all the attached Members on each submit, but that seems like too much brute force. I need a way to update the attributes of each member, but I can''t think of a good way to match up params[:member].each with @app.members Thanks, --Wilson.
Wilson I missed the single action going by.. can you send me what you are doing... I''m working on stuff in a similar area and would love to see different wayws of tackling the problem... Wilson wrote:> With the gracious help of DHH, I now have a single action working that > allows me to create multiple Members on an App, without any tedious > "add another" submit-button clicking on the part of the user. Cool stuff._______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sure. You can find some of the relevant code here: http://supremetyrant.com/misc/rails/ Along the way, I discovered what I''m pretty sure is a Rails bug. I needed the output on the partial form to look like this: <select class="required" id="member_<%= member_counter %>_sex_code" name="member[<%= member_counter %>][sex_code]"> <option></option> <option value="M">Male</option> <option value="F">Female</option> <option value="U">Unknown</option> </select> ..however, I wanted to use the form helper, because that enables the fun error highlighting. According to the docs, this is the proper form: select ''object'', ''method'', [choices as an array], {options as a hash}, {html options as a hash} e.g.: <%= select ''member'', ''sex_code'', [[''Male'',''M''], [''Female'',''F''], [''Unknown'',''U'']], {''index'' => member_counter}, {''class'' => ''required''} %> However, unlike the methods in form_helper.rb, which consider "index" to be an "option", the methods in form_options_helper.rb consider it to be an "html option". Therefore, the working syntax requires a placeholder hash: <%= select ''member'', ''sex_code'', [[''Male'',''M''], [''Female'',''F''], [''Unknown'',''U'']], {}, {''index'' => member_counter, ''class'' => ''required''} %> I haven''t seen that documented anywhere, so I thought I''d mention it. Sean T Allen wrote:> Wilson I missed the single action going by.. > > can you send me what you are doing... I''m working on stuff in a similar > area and would love > to see different wayws of tackling the problem... > > Wilson wrote: > >> With the gracious help of DHH, I now have a single action working that >> allows me to create multiple Members on an App, without any tedious >> "add another" submit-button clicking on the part of the user. Cool stuff. > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
So params[:members] still comes through as a hash yes? Wilson wrote:> Sure. You can find some of the relevant code here: > http://supremetyrant.com/misc/rails/ >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Someone else feel free to chime in if they understand this better, but I''m feeding @members to the form as an Array, and getting back params[:member] (not :members) as a Hash with numbers stored as strings as the keys. Sean T Allen wrote:> So params[:members] still comes through as a hash yes? > > Wilson wrote: > >> Sure. You can find some of the relevant code here: >> http://supremetyrant.com/misc/rails/ >>
sorry should have been params[:member] hmmm well i was handcoding in the bit with index... so i definitely learned something there... thanks wilson Wilson wrote:> Someone else feel free to chime in if they understand this better, but > I''m feeding @members to the form as an Array, and getting back > params[:member] (not :members) as a Hash with numbers stored as > strings as the keys. > > Sean T Allen wrote: > >> So params[:members] still comes through as a hash yes? >> >> Wilson wrote: >> >>> Sure. You can find some of the relevant code here: >>> http://supremetyrant.com/misc/rails/ >>> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails