Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: form_tag validation

Can I use ActiveRecord-like form validation when using form_tag and not form_form (hence a for not bound to a model)? How?

like image 629
pistacchio Avatar asked Jan 10 '11 06:01

pistacchio


3 Answers

You can't have validations with form_tag as your form is not bound to a model.

But the good part is that with Rails 3 and ActiveModel you can create models that are not bound to your database. So you can create "virtual" models with validations for search forms, contact forms, etc.

See : https://github.com/novagile/basic_active_model

like image 181
Nicolas Blanco Avatar answered Nov 02 '22 13:11

Nicolas Blanco


As form_tag does not bound to any models, so you have to validate the data and write code for displaying the errors by yourself.

Or you could try to create non-database-backend models with validations and use form_for instead.

like image 29
PeterWong Avatar answered Nov 02 '22 13:11

PeterWong


This works well for me in Rails 3.0.9:

<%= form_tag("/events", :id => "new_event") do %>
  <%= error_messages_for @event %>
  <%= submit_tag "Submit" %>
<% end %>
like image 32
BenH Avatar answered Nov 02 '22 12:11

BenH