Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

OpenStruct objects can no longer be used in form helpers

#5508

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

Reported by Lars Kuhnt · August 31st, 2010 @ 09:04 AM

State: wontfix
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Samuel Kadolph
    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 in new then it generates accessor methods.

    <%= form_for OpenStruct.new(:message => ''), :url => ... do |f| %>
      <%= f.text_field :message %>
    <% end %>
    

    Works fine.

    August 31st, 2010 @ 02:25 PM

  2. bhf
    bhf

    That's what i needed for lipodrene course too!

    August 31st, 2010 @ 04:32 PM

  3. Lars Kuhnt
    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
    

    August 31st, 2010 @ 05:57 PM

  4. Samuel Kadolph
    Samuel Kadolph
    • Assigned user set to Santiago Pastorino

    The code was changed to check for method_name and method_name + '_before_type_cast otherwise raise an exception in this commit:

    Make InstanceTagMethods#value_before_type_cast raise if the model don't respond to attr_before_type_cast or attr method


    The commit was to solve these two tickets:

    1. text_field() does not use overridden model accessors
    2. FormHelper problem with write-only fields

    Maybe Santiago can provide some insight.

    August 31st, 2010 @ 06:23 PM

  5. Santiago Pastorino
    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

    September 1st, 2010 @ 02:49 AM

  6. Lars Kuhnt
    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.

    September 1st, 2010 @ 09:06 AM