0

I'm trying to implement a custom Fake Input (found in this article) with a Simple Form For. This is my code:

<%= simple_form_for :results, { url: admin_add_sites_path } do |f| %>
  <%= f.input :no_merchandisers, as: :fake %>
  <%= f.input :site_codes_to_add, as: :fake %>
  <%= f.submit "Submit" %>
<% end %>

When I first implemented the input I received an error:

No input found for fake

The answer in this Stack overflow question tells me to restart my server. This temporarily fixes the above issue for me.

But, whenever I edit and save a file (it was happening when editing a controller file), the error pops up again and I have to restart my server to be able to continue. Has anyone else experienced this before? Is there a fix for this?

Community
  • 1
  • 1
Jonathan Yeong
  • 523
  • 1
  • 4
  • 8
  • I haven't heard about "fake" inputs, but try adding `value: nil` to your inputs. That might work. – Ryan K Mar 22 '16 at 02:43
  • Sorry to clarify; from the Fake input article: "Sometimes I just need a custom input for extra params But all the existing inputs read from object's attributes". I use it because `:no_merchandisers`, and `:site_codes_to_add` are not columns in a model. But I still want to capture their data. – Jonathan Yeong Mar 22 '16 at 03:53

1 Answers1

0

There are a few solutions to this problem that I use regularly (though I don't use Simple Form). First is to use value: nil. All this does is overrides the default value method that would normally look for the model attribute. It still submits with the rest of the form.

<%= f.input :no_merchandisers, value: nil %>

The second solution is to use a "tag" input, depending on what your input actually is:

<%= text_field_tag "results[no_merchandisers]", value_variable %>

or maybe:

<%= check_box_tag "results[no_merchandisers]", value_variable, checked_variable %>

Now, neither of these solutions fixes the as: :fake problem, just replaces it entirely. I'm not familiar with Simple Form, so I don't know how that additional code would (or wouldn't) work.

Ryan K
  • 3,985
  • 4
  • 39
  • 42
  • I think using the tag option and then styling the form without using Simple Form may be the way to go. Simple Form is just a wrapper around `form_for` which expects to take in an model. If I wanted to have a form without needing a model then the "tag" input would be the solution I guess. Thanks for taking the time to answer that Ryan. – Jonathan Yeong Mar 22 '16 at 22:58