SimpleForm without (not a model)

Is it possible to use Simple Form (by: Plataformatec) without a model?

https://github.com/plataformatec/simple_form

+64
ruby-on-rails ruby-on-rails-3 simple-form
Mar 03 2018-11-11T00:
source share
5 answers

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> 
+104
Mar 03 2018-11-11T00:
source share

Unfortunately, simple_form relies on using a model. In fact, it would be nice to have something like the simple_form_tag and input_tag methods equivalent to their * _tag helper rails. Until then, it’s easy to work there.

Use a character instead of a class in the form and pass the value explicitly to prevent simple_form from trying to access model properties.

 <%= simple_form_for :user, :url => '/users' do |f| %> <%= f.text_field :name, input_html: { value: nil } %> <% end %> 

This will avoid the undefined method 'name' for User error.

+13
Jul 02 '13 at 19:38
source share

You can also use fields outside the model as part of the form model using simple_fields_for something like this:

 <%= simple_form_for @user do |f| %> <%= f.input :name %> <%= simple_fields_for :no_model_fields do |n| %> <%= n.input :other_field %> <% end %> <% end %> 

This is a simple and practical solution, because you can create different fields from different models or without using models.

+4
Aug 05 '16 at 16:02
source share

All of the above methods still leave you with form data nested inside the "user" or any other character that you pass as the first argument. This is annoying.

To mimic the style / benefits of simple_form, but remove the object / symbol dependency and nested data nesting, you can create a partial one.

Examples

HAML :

form view:

 = form_tag("path/to/action", method: "POST") do = render "path/to/partial/field", type: "string", required: true, item: "first_name" 

field partial:

 - required_string = required ? "required" : "" %div{class: "input #{type} #{required_string} #{item}"} %label{class: "#{type} #{required_string}", for: "#{item}"} - if required %abbr{title: "required"} * = t("application.#{item}") %input{name: "#{item}", | placeholder: t("application.#{item}"), | type: "#{type}", | required: required, | "aria-required" => "#{required}" } 
+3
Nov 17 '14 at 17:44
source share

You can also pass :symbol instead of @object as an argument to simple_form_for .

 <%= simple_form_for :email, :url => '/post_email' do |f| %> <%= f.input :subject, :as => :string %> <% end %> 

What will be output:

 <form method="post" class="simple_form email" action="/post_email" accept-charset="UTF-8"> ... <input type="text" size="30" name="email[subject]" id="email_subject"> </form> 

Please note the following backlinks:

  • You cannot use automatic model checking
  • You must explicitly define :url and type of each input
+2
Mar 06 '13 at 23:59 on
source share



All Articles