Multiple objects in a Rails form

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I want to edit multiple items of my model photo in one form. I am unsure of how to correctly present and POST this with a form, as well as how to gather the items in the update action in the controller.

This is what I want:

The parameters are just an example, like I stated above: I am not sure of the best way to POST these values in this form.

In the controller I want to something like this:

@photos = Photo.find( params[photos] ) @photos.each do |photo|     photo.update_attributes!(params[:photos][photo] ) end 

回答1:

In Rails 4, just this

                       ... other photo fields 


回答2:

UPDATE: This answer applies to Rails 2, or if you have special constraints that require custom logic. The easy cases are well addressed using fields_for as discussed elsewhere.

Rails isn't going to help you out a lot to do this. It goes against the standard view conventions, so you'll have to do workarounds in the view, the controller, even the routes. That's no fun.

The key resources on dealing with multi-model forms the Rails way are Stephen Chu's params-foo series, or if you're on Rails 2.3, check out Nested Object Forms

It becomes much easier if you define some kind of singular resource that you are editing, like a Photoset. A Photoset could be a real, ActiveRecord type of model or it can just be a facade that accepts data and throws errors as if it were an ActiveRecord model.

Now you can write a view form somewhat like this:

                                       

Your model should validate each child Photo that comes in and aggregate their errors. You may want to check out a good article on how to include Validations in any class. It could look something like this:

class Photoset   include ActiveRecord::Validations   attr_accessor :photos    validate :all_photos_okay    def all_photos_okay     photos.each do |photo|       errors.add photo.errors unless photo.valid?     end   end    def save     photos.all?(&:save)   end    def photos=(incoming_data)     incoming_data.each do |incoming|        if incoming.respond_to? :attributes          @photos 

By using a facade model for the Photoset, you can keep your controller and view logic simple and straightforward, reserving the most complex code for a dedicated model. This code probably won't run out of the box, but hopefully it will give you some ideas and point you in the right direction to resolve your question.



回答3:

Rails does have a way to do this - I don't know when it was introduced, but it's basically described here: http://guides.rubyonrails.org/form_helpers.html#using-form-helpers

It took a bit of fiddling to alter the configuration properly for the case where there's no parent object, but this seems to be correct (it's basically the same as gamov's answer, but cleaner and doesn't allow for "new" records mixed in with the "update" records):

                         ... [other fields]          

In your controller, you'll end up with a hash in params[:photos], where the keys are photo IDs, and the values are attribute hashes.



回答4:

You can use "model name[]" syntax to represent multiple objects.

In view, use "photo[]" as a model name.

 photos_update_path do |f| %>         "photo_form", :locals => {f => f} %>          

This will populate input fields just like you described.

In your controller, you can do bulk updates.

def update   Photo.update(params[:photo].keys, params[:photo].values)   ... end  


回答5:

Indeed, as Turadg mentioned, Rack (Rails 3.0.5) fails if you mix new & existing records in Glen's answer. You can work around this by making fields_for work manually:

                          ... [other photo fields]     

This is pretty ugly if you ask me, but it's the only way I found to edit multiple records while mixing new and existing records. The trick here is that instead of having an array of records, the params hash gets a array of hashes (numbered with i, 0,1,2, etc) AND the id in the record hash. Rails will update the existing records accordingly and create the new ones.

One more note: You still need to process the new and existing records in the controller separately (check if :id.present?)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!