How do you get the Ruby on Rails generated id of a form element for reference in JavaScript?

前端 未结 8 875
無奈伤痛
無奈伤痛 2021-02-05 01:01

When using the form_for helper and a text_field call, Ruby on Rails will generate a unique id for the element that it outp

8条回答
  •  时光说笑
    2021-02-05 01:43

    In Rails 4.1 I was able to get the id with this:

    ActionView::Base::Tags::Base.new(
      f.object_name,
      :username,
      :this_param_is_ignored,
    ).send(:tag_id)
    

    It works with nested forms (fields_for).

提交回复
热议问题