Rails Simpleform with Non-Model Inputs

Rails Simpleform with non-model inputs

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

SimpleForm without for (non model form)

You can use :symbol as the first argument.

<%= simple_form_for :user, url: users_path do |f| %>
<%= f.input :name, as: :string %>
...
<% end %>

It will output something like this:

<form novalidate="novalidate" class="simple_form user" action="/users" accept-charset="UTF-8" method="post">
...
<div class="input string required user_name">
<label class="string required" for="user_name">
<abbr title="required">*</abbr> Name
</label>
<input class="string required" type="text" name="user[name]" id="user_name" />
</div>
...
</form>

How can I add non-model fields to a simple_form?

In you model you can add:

attr_accessor :something, :something_else

rails simple_form fields not related to the model

You can use attr_accessor

 class Order < ActiveRecord::Base

attr_accessor :card_number


end

Now you can do Order.first.card_number = '54421542122' or use it in your form or whatever else you need to do.

See here for ruby docs http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-attr_accessor
and here for a useful stackoverflow question What is attr_accessor in Ruby?

Don't get it mixed up with attr_accessible! Difference between attr_accessor and attr_accessible

simple_form a single non-model field

Not sure what you can't do, but i do something like:

$(".datepicker").datepicker({ altFormat: "yy-mm-dd", dateFormat: "yy-mm-dd" });

If configuring the datepicker doesn't work for you, use attr_accessor :new_field_name in your model and add the datepicker to it and do what you were going to do there.

How to configure a SimpleForm input to default to no wrapper

Apart from the option you already mentioned in your question, of just using input_field instead of input (more here: https://github.com/plataformatec/simple_form#stripping-away-all-wrapper-divs), you may define your wrapper like this (untested):

SimpleForm.setup do |config|
config.wrapper_mappings = {
my_custom: :wrapper_false,
}

config.wrappers :wrapper_false, tag: false do |b|
b.use :placeholder
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
end
end

And then in your forms:

<%= f.input :attribute, as: :my_custom %>

More on the wrappers API: https://github.com/plataformatec/simple_form#the-wrappers-api

EDIT:

After considering the case you mentioned, I sent a PR to simple_form project to allow for wrapper: false to be configured in wrapper_mappings. If it gets accepted, you should be able to disable the wrapper directly in the initializer like this:

config.wrapper_mappings = {
my_custom: false
}

What benefits does simple_form provide when used without a model?

The benefit that it provides is that you don't have to build any html around your fields, and add classes to them, if you properly configured simple_form. Because for example for bootstrap you mostly will have some wrappers around your inputs.

And you also have automatic error messages in your form this way.

how to specify label for select in simpleform rails

This is because f.select is not a simple_form method and does not support :label

This should work for you w/ simple form

<%= f.input :source_type, :label => "Lead or VTeam", :collection => ["lead","vteam"], :selected => "lead" %>

Hope this helps



Related Topics



Leave a reply



Submit