jbehrens94
jbehrens94

Reputation: 2406

Rails 4 form without model and database

I want to create a form in HAML, but I don't have a model or database table. The form should just send its fields via POST to an action and I will handle it from there. As far as I could tell, form_for has to have a record.

What is the current best practice to do this?

Upvotes: 23

Views: 24422

Answers (1)

Josh
Josh

Reputation: 5721

form_for can also take an arbitrary symbol:

<%= form_for :anything, url: "my_controller/my_action" do |form| %>
  <%= form.text_field :name %>
  <%= form.submit  %>
<% end  %>

This will send a post to my_controller/my_action.

The html output will look something like this:

<form accept-charset="UTF-8" action="my_controller/my_action" method="post">
    <input id="anything_name" name="anything[name]" type="text">
    <input name="commit" type="submit" value="Save Testing">
</form>

Upvotes: 55

Related Questions