Update multiple records rails
I recently had a requirement to create a single form with data from more than one record. This is a simple request, but I had never done it before in Rails. After experimenting for a while and reading quite a few forum posts and StackOverflow answers I came up with a working solution. Hopefully this will save someone a little time in the future. Each user will have a first name, last name, and e-mail address.
Next, add the new routes for editing all users. I tried to stick to the RESTful convention with these. The only thing it needs to do is get all users. The interesting part of this code starts around line 9. As expected, we iterate over the users with users. The next line tells Rails to name the fields for each user with array notation.
The key for each of these hashes will be the user id. We iterate over each key in params['user'] , then find and update the user associated with that id. I am leaving error checking as an exercise for the reader…. Hey, great article. Viewed 40k times. What I've found so far is something like Model. This feels really junior, but I've googled and looked around SO and haven't found my answer. Add a comment.
Active Oldest Votes. Brett Bender Brett Bender Yeah, that's what I'm looking for. Bryce Haha no worries, there's too much manual to read it all if you want to get anything done ; — Brett Bender.
Show 1 more comment. Taryn East Taryn East It is extremely unwise to perform string interpolations within a query string. This was written in Keep that in mind and update it accordingly : — Taryn East. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 1.
0コメント