This project is archived and is in readonly mode.
OpenStruct objects can no longer be used in form helpers
Reported by Lars Kuhnt | August 31st, 2010 @ 09:04 AM
Hi,
with rails 3 it is no longer possible to use OpenStruct objects as flexible models in form helpers.
For example:
<%= form_for OpenStruct.new, :url => notifications_path do |f| %>
<%= f.text_field :message %>
<% end %>
raises the following error:
Model OpenStruct does not respond to message
I always liked the possibility to use OpenStruct objects in form helpers.
Regards
Lars
Comments and changes to this ticket
-
Samuel Kadolph August 31st, 2010 @ 02:25 PM
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 innew
then it generates accessor methods.<%= form_for OpenStruct.new(:message => ''), :url => ... do |f| %> <%= f.text_field :message %> <% end %>
Works fine.
-
Lars Kuhnt August 31st, 2010 @ 05:57 PM
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 August 31st, 2010 @ 06:23 PM
- Assigned user set to Santiago Pastorino
The code was changed to check for
method_name
andmethod_name + '_before_type_cast
otherwise raise an exception in this commit:
The commit was to solve these two tickets:
Maybe Santiago can provide some insight.
-
Santiago Pastorino September 1st, 2010 @ 02:49 AM
- State changed from new to wontfix
- Importance changed from to Low
You should check this whole thread https://rails.lighthouseapp.com/projects/8994/tickets/5471 and let me know if you have more concerns
-
Lars Kuhnt September 1st, 2010 @ 09:06 AM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>