-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to use with Arrays and non-associations #90
Conversation
This seems like great work. I will have an in depth look at it tonight. Indeed, if I understand correctly, this could handle the array-fields as well, if somebody would add the correct method |
Yes, exactly. If you'll accept it, we would have to add it to README too I suppose. On 24/09/2012, at 21:37, Nathan Van der Auwera notifications@github.com wrote:
|
Allow to use with Arrays and non-associations
@nathanvda thanks for merging it in. I wonder when are you planning to release it? |
I want to update my demo-project and then I will release it. I hope to finish that on wednesday. |
I tried this way to handle array fields and it works with "link_to_add_association". I'm just not sure of what should be in "build_[array field]" method and what's the difference between "build_[singular array field]" and "build_[plural array field]" as noted into specs. I've tried this and it seems to work but it doesn't seem very clean to me : def build_paths
{ :"" => "" }
end I get an error if I provide an array since the field name method doesn't exsits. A small example would be nice ;-) Note that, for array fields, the field has to be set like this in the partial : The big issue is that For now, I juste don't use |
There's another problem : I don't know how to recover existing array values in the form. Changing def build_paths
paths
end Actually, the purposed design doesn't really fit with array fields since you're waiting for a 'virtual' association and field names. But an array has a single field with no name. My initial (not very clean) pull request was made to specifically handle this case. I don't see how to do it with this new feature. It's made for non AR compatible models but a model with fields is still required... |
I will try to add an example later this evening or tomorrow morning. |
@porecreat here's an example (stripped down a bit). All you need is the class CompanyInvitation < ActiveRecord::Base
attr_accessor :additional_participations
def additional_participations
@additional_participations ||= parse_additional_participations
end
def build_additional_participation
Participation.new # Required by cocoon for the nested forms - return a single instance of a new object
end
def additional_participations_attributes=(values)
@additional_participations = values.each.map do |k, params|
params.delete('_destroy')
# Convert user_email, user_fn etc into {user => email, fn etc}
user_params = Hash[ Participation::USER_FIELDS.map{|f| [f, params["user_#{f}"]]} ]
user_params['password_confirmation'] = user_params['initial_password'] = user_params['password']
Participation.new(params.merge('user_attributes' => user_params)).tap do |p|
p.company = company
end
end
end
end |
I know this is a little old but... thank you @dnagir! You save my life 👍 |
Can we have the same feature for |
This complements already accepted PR nathanvda#90 That PR allowed using cocoon on ActiveRecord models with nested Array items. But still the main form object had to be ActiveRecord. This commit removes that restriction and allows using cocoon with plain ActiveModel-compliant objects (including the object for the main form). This has been requested in the issue below but went unnoticed nathanvda#141 Usage example (model only side) ```ruby class User include ActiveAttr::Model attribute :email end class Company include ActiveAttr::Model def users @users ||= [] end # This will allow setting the attributes from `fields_for` rails helper # when the form will be posted def users_attributes=(attrs) # update the users array... end # cocoon uses this method to render the template def build_user User.new # blank state for new template end end ```
This PR allows to use cocoon with custom methods and doesn't bind it to AR only.
So ActiveAttr or just transient methods can be used.
build_<association>
.build_<association>
method is defined.create_object
May deal with the related issues: #79, #85, #86