Skip to content
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

control_group method fails on nested attributes in serialized attributes #23

Closed
carlosdavis opened this issue Sep 11, 2013 · 9 comments
Closed

Comments

@carlosdavis
Copy link

The control_group method calls object.errors, which will fail if object is nil, or if object doesn't respond to errors.

This is valid Rails form syntax, which fails under bootstrap_form_for:

= f.fields_for :options do |builder|
  = builder.text_field :color
  = builder.text_field :size
@carloslopes
Copy link
Member

I think you misunderstood the usage of control_group. It isn't an alternative to fields_for neither similar to it.

Here is an explanation of what is control_group for Bootstrap.

I'll close this issue now but if you have any other doubt ask me!

@carlosdavis
Copy link
Author

I'm sorry: I didn't provide enough specificity in my issue report. I'm not calling control_group directly. control_group gets called via FORM_HELPERS.each{|m| define_method etc.}:
https://github.com/potenza/bootstrap_form/blob/master/lib/bootstrap_form/form_builder.rb#L24

When that chain of methods executes on a key/value pair within a serialized hash (via fields_for), your code for object.errors throws an exception because that serialized hash item does not respond to .errors (especially if that item is nil).

@carlosdavis
Copy link
Author

Let me know if you need a stack trace or full example code.

@carloslopes
Copy link
Member

Hm yes, it would be nice if you create a gist with a stack trace and an example code 😉

@carloslopes carloslopes reopened this Sep 12, 2013
@carlosdavis
Copy link
Author

@potenza
Copy link
Member

potenza commented Sep 12, 2013

Thanks. I'm taking a look at this now. I believe it may have been fixed in the 1.0 release, but I want to verify (from the stack trace, I see you're using 0.3.2).

@carlosdavis
Copy link
Author

Ah, thank you so much. We're on Rails 3—is the 1.0 release only for Rails 4?

@potenza
Copy link
Member

potenza commented Sep 12, 2013

You should be fine on 1.0 (I should note this in the README). In Rails 4, sending a block to FormBuilder.new is deprecated. To handle this, we just default the block to nil:

https://github.com/potenza/bootstrap_form/blob/master/lib/bootstrap_form/form_builder.rb#L10

I also created a Rails 3 test project with bootstrap_form 0.3.2 and was able to reproduce the error. Upgrading to 1.0 fixed it.

FYI, I had to explicitly set the text_field values to pull them from the preferences hash:

<%= bootstrap_form_for @user do |f| %>
  <%= f.text_field :email %>
  <%= f.fields_for :preferences do |builder| %>
    <%= builder.text_field :favorite_color, value: @user.preferences[:favorite_color] %>
    <%= builder.text_field :favorite_animal, value: @user.preferences[:favorite_animal] %>
  <% end %>
<% end %>

If you know the keys in advance, it might be simpler to define getters/setters for the preferences keys and skip fields_for altogether.

@potenza
Copy link
Member

potenza commented Sep 13, 2013

I'm gonna close this for now, but let me know if you're still having issues. Thanks!

@potenza potenza closed this as completed Sep 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants