dace
dace

Reputation: 6363

Translate form classes and ids to rails form_tag fields

I have a bootstrap theme that I'm using. In their theme they are applying styles through CSS selectors, but through an HTML form. I basically want to transfer their attributes over to a form_tag so that the styling remains intact with their CSS.

<form name="sentMessage"  novalidate="">

  <p>Your E-mail</p>
  <input type="text" class="form-control" id="email" required="" data-validation-required-message="Enter Email">

  <p>Your Password</p>
  <input type="email" class="form-control" id="password" required="" data-validation-required-message="Enter Password">

  <button type="submit" class="btn btn-default pull-right send">Log In</button>

I'd like to apply those same styles over to this rails form_tag, but am having a little trouble doing so:

<% if @user.errors.any? %>
<ul>
  <% @user.errors.full_messages.each do |error| %>
    <li><%= error %></li>
  <% end %>
</ul>
<% end %>

<%= form_tag sign_in_path do %>
  <div>
    <%= label_tag :email %>
    <%= email_field_tag :email %>

    <%= label_tag :password %>
    <%= password_field_tag :password %>

  <%= submit_tag "Sign in!" %>
  <%= link_to "Not a member yet? Sign up", sign_up_path %>
<% end %>

The main issue is translating the name from the HTML form over to the form_tag (do I simply just include it as name: "sentMessage"?). I've also tried applying the class and id tags from the HTML form over to the field tags, like this (but am not sure if it is correct):

<%= form_tag sign_in_path do %>
  <div>
    <%= label_tag :email %>
    <%= email_field_tag :email, class: "form-control", id: "email"  %>

    <%= label_tag :password %>
    <%= password_field_tag :password, class: "form-control", id: "password" %>

  <%= submit_tag "Sign in!" %>
  <%= link_to "Not a member yet? Sign up", sign_up_path %>
<% end %>

Upvotes: 1

Views: 197

Answers (1)

bhanu
bhanu

Reputation: 2460

You can pass options hash to the form tag additional to the form action, as well as class attribute to the submit_tag, and for input fields you have to specify the value as second argument before specifying any html options, so your form goes like this

<%= form_tag sign_in_path, {name: "sentMessage", novalidate:""} do %>
  <%= label_tag :email %>
  <%= email_field_tag :email, '', class: "form-control", id: "email"  %>

  <%= label_tag :password %>
  <%= password_field_tag :password, '', class: "form-control", id: "password" %>

  <%= submit_tag "Sign in!", class:"btn btn-default pull-right send" %>
  <%= link_to "Not a member yet? Sign up", sign_up_path %>
<% end %>

Upvotes: 1

Related Questions