Search code examples
ruby-on-rails-4draper

Draper causing text_field to output incorrect results


I'm using draper to clean up some view logic. I have some code as such:

Decorator:

class EventTypeDecorator < Draper::Decorator
  delegate_all

  def name_field
    if object.name == 'Miscellaneous'
      h.text_field object, :name, {id: "event_type_#{object.id}_name", disabled: true}
    else
      h.text_field object, :name, {id: "event_type_#{object.id}_name"}
    end
  end
end

Form:

= form_for event_type, remote: true do |f|
  ...
  = f.label :name, 'Event type name:'
  ...

Output:

<input disabled="disabled" id="event_type_1_name" name="#<EventType:0x007f9db6df9cf0>[name]" type="text">

Expected output:

<input value="Miscellaneous" disabled="disabled" id="event_type_1_name" name="event_type[name]" type="text">

I'm not entirely sure what is going on here. Can anyone offer some help? Everything was working fine before I was using draper and had the logic in the template. I also don't know a lot of ruby so I might be missing something obvious, sorry!


Solution

  • Turns out I was using the text_field tag incorrectly. The proper usage is: h.text_field :event_type, :name, {id: "event_type_#{object.id}_name", value: object.name, disabled: true}

    I needed to add the :event_type symbol because I was previously using a form_for with f.text_field which provides the object_name argument for you!

    Simple mistake.