2015-05-31 40 views
1

我有一個引導主題,我正在使用。在他們的主題中,他們通過CSS選擇器應用樣式,但通過HTML表單。我基本上想要將它們的屬性傳遞給一個form_tag,以便樣式保持與CSS完好無損。將窗體類和ID轉換爲軌道form_tag字段

<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> 

我想在這個軌道的form_tag應用這些相同的樣式,但我有一個小麻煩這樣做:

<% 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 %> 

主要問題是從翻譯名稱將HTML表單轉換爲form_tag(我只是簡單地將它包含爲名稱:「sentMessage」?)。我也試着從HTML應用class和id標籤形成了到外地標籤,像這樣的(但我不知道這是否是正確的):

<%= 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 %> 

回答

1

你可以通過選擇哈希形式標籤額外的表單操作,以及class屬性的submit_tag,並輸入字段,你必須指定任何HTML選項之前指定的值作爲第二個參數,所以你的形式是這樣的

<%= 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 %> 
+0

感謝巴努• - 正是我在找什麼。工作很好,很感激! – hidace