Rails Simpleform with non-model inputs

点点圈 提交于 2019-11-29 01:23:18

Why don't you add:

attr_accessor :attr

to your model's class definition? This way your code:

<%= f.input :attr %>

should work.

OR

If this solution isn't suitable, you can always pass some value to your input method directly:

<%= f.input :attr, input_html: {value: 'something'}  %>

Say you wanted to use a rails form helper but still wrap it in SimpleForm goodness? You can, by calling input with a block like so:

<%= simple_form_for @obj do |f| %>
  <%= f.input :name %>
  <%= f.input :attr do %>
    <%= text_field_tag 'attr' %>
  <% end %>
<% end %>

Yes, below are quote from simple_form wiki

String Input

app/inputs/fake_input.rb:

class FakeInput < SimpleForm::Inputs::StringInput
  # This method only create a basic input without reading any value from object
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    template.text_field_tag(attribute_name, nil, merged_input_options)
  end
end

Then you can do <%= f.input :thing, as: :fake %>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!