Ticket #504 (closed defect: duplicate)

Opened 10 months ago

Last modified 10 months ago

merb_helpers: text_control requires extraneous objects

Reported by: jamie.devja..@tracefunc.com Assigned to:
Priority: major Milestone: The Future
Component: Plugins Keywords:
Cc:

Description

I would like to display a create form like so:

<% form_for :customer, :action => Customers.url_for(:create) do %>
  <%= text_control :name, :label => 'New Customer: ' %>
  <%= submit_button 'Create' %>
<% end %>

However, this requires I assign @customer = Customer.new in my action in order to generate the default value for the fields.

Creating this dummy object is a bit of a pain. If Merb::Helpers::Form#control_value checked for nil of @_obj, then the form will function if I don't want to provide that information.

Proposed fix, /plugins/merb_helpers/lib/merb_helpers/form_helpers.rb line 154:156:

      def control_value(col) #:nodoc:
        @_obj.send(col) if @_obj
      end

Change History

02/10/08 14:42:07 changed by jamie.devja..@tracefunc.com

Turns out previous solution was outputting a broken form. I got it working in browser and in specs by overriding obj_from_ivar_or_sym instead. If there's no instance var of the appropriate name, it'll try to create the class instance.

      def obj_from_ivar_or_sym(obj) #:nodoc:
        if obj.is_a?(Symbol)
          an_obj = instance_variable_get..@#{obj}")
          if an_obj.nil?
            an_obj = obj.to_s.classify.constantize.new
          end
          an_obj
        else
          obj
        end
      end

If merb_helpers had tests/specs, I'd include some too :P

02/11/08 13:28:26 changed by shayarne..@gmail.com

  • status changed from new to closed.
  • resolution set to duplicate.