Rails Simpleform with non-model inputs

11,700

Solution 1

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'}  %>

Solution 2

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 %>

Solution 3

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 %>

Share:
11,700

Related videos on Youtube

lucas clemente
Author by

lucas clemente

Updated on July 03, 2022

Comments

  • lucas clemente
    lucas clemente almost 2 years

    I have a normal form using simpleform. Now I'd like to add an input that does not have any corresponding field in the model, it will be processed by the controller. I tried

    <%= simple_form_for @obj do |f| %>
      <%= f.input :name %>
      <%= f.input :attr, as: :string %>   <-- should just send "attr" as post data
    <% end %>
    

    but this gives a Method not found: attr_not_in_obj error. I could obviously use the standard rails helpers, but then I will miss all of the simpleform HTML around the input, and copying doesn't quite seem right.

    In short: I'm looking for something like simpleform version of rails tag helpers, without any connection to a model. How do I add inputs that do not correspond to model attributes?

  • lucas clemente
    lucas clemente almost 12 years
    But it doesn't really belong in the model, it's something that only the controller should be aware of.
  • jdoe
    jdoe almost 12 years
    @lucasclemente Updated my answer.
  • lucas clemente
    lucas clemente almost 12 years
    The second option is just the default value, how does this affect the problem?
  • lucas clemente
    lucas clemente almost 12 years
    I'm looking for something like simpleform version of rails tag helpers, without any connection to a model.
  • jdoe
    jdoe almost 12 years
    @lucasclemente It solves the problem, because no call for attr method will be performed on @obj if you specified some value for it. Put '' (empty string) instead of 'something' if this bothers you. Just try it.
  • Ryan Sandridge
    Ryan Sandridge over 11 years
    I'm trying to adapt this solution for a collection. Any idea how to do that?
  • Ryan Sandridge
    Ryan Sandridge over 11 years
    Nevermind... see my solution to the original question, which solved my problem (as well as @lucasclemente 's).
  • JosephK
    JosephK over 9 years
    Wish someone had answered the original question.
  • freemanoid
    freemanoid over 9 years
    That's a good idea but all related simple_form css classes like boolean, optional and proper id would not be set. It builds a proper label though.