Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a form in Rails without having to use form_for and a model instance?

First of all, I'm a Rails newbie. I can hold my own in Ruby, but Rails is a whole different story for me. I like the development speed Rails offers me, but I can't seem to make peace with the existing documentation.

For all of my forms so far, I used form_for, with an instance for the model I needed to create ( for example, submitting a new book ). I would really like to be able to just write something like :


<% form(:action => "whatever") %>
  <% text_field ... %>
  <% file_field ... %>
<% end %>

From the articles I read online, I understood that this was the way things got done in Rails < 2.0 . Is there anyway of doing this in Rails > 2.0, or something equivalent to it ? Can you please post a snippet ?

like image 874
Geo Avatar asked Apr 29 '09 21:04

Geo


People also ask

What is a form helper in Rails?

1.3 Helpers for Generating Form Elements Rails provides a series of helpers for generating form elements such as checkboxes, text fields, and radio buttons. These basic helpers, with names ending in _tag (such as text_field_tag and check_box_tag ), generate just a single <input> element.

What is form_ with in Rails?

form_with syntax. form_with is a Rails form helper, similar to form_tag and form_for which have both been soft deprecated. It is a form helper that allows us to use ruby code to build an HTML form. form_with can bind a form to a model object or it can create a simple form that does not require a model.


1 Answers

Take a look at form_tag.

<% form_tag '/posts' do %>
  <div><%= submit_tag 'Save' %></div>
<% end %>
like image 158
Jim Puls Avatar answered Nov 01 '22 15:11

Jim Puls