This project is archived and is in readonly mode.
OpenStruct objects can no longer be used in form helpers
-
Samuel Kadolph
The helpers are checking if the object
responds_to?(:message)now which returns false. OpenStruct uses method_missing to add new attributes unless you specify them innewthen it generates accessor methods.<%= form_for OpenStruct.new(:message => ''), :url => ... do |f| %> <%= f.text_field :message %> <% end %>Works fine.
-
Lars Kuhnt
Thanks Samuel, but I was wondering why rails 3 forces me to define the attributes specifically. If the object does not respond to the called method, an undefined method error is raised anyhow. So whats the point in calling respond_to in the form helper?
Anyway, I fixed it by monkey-patching OpenStruct#respond_to?
# config/initializers/open_struct_extensions.rb class OpenStruct def respond_to?(symbol, include_private = false) true end end -
Samuel Kadolph
- Assigned user set to Santiago Pastorino
The code was changed to check for
method_nameandmethod_name + '_before_type_castotherwise raise an exception in this commit:
The commit was to solve these two tickets:
Maybe Santiago can provide some insight.
-
Santiago Pastorino
- State changed from new to wontfix
- Importance changed from to Low
You should check this whole thread #5471 FormHelper problem with write-only fields and let me know if you have more concerns
-
Lars Kuhnt
Thanks for the effort and clarification.
Maybe OpenStruct should override respond_to? like above because it responds to all methods, but that does not belong here.