[英]Rails form_for, having a text_field without a label (without using CSS)
[英]Ruby on Rails - adding CSS to text_field
我在Rails中創建了一個表單,當我向text_field添加一個CSS類時,表單將不會提交。
我還是鐵桿新手,所以任何幫助都會非常感激。
這是我的表格:
<% @page_title = "Contact" %>
<hr>
<div class="col-lg-12">
<h1 class="page-header">
BK Legal Courier Corp <br />Call BK @ <span style="color:#47B224">613-286-8265</span>
</h1>
</div>
<hr>
<div class="container">
<h2>Contact Us</h2>
<%= form_for :fc, :url => {:action => 'create'} do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field (:first_name, :class => 'form-control')%>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field (:last_name, :class => 'form-control') %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field (:email, :class => 'form-control') %>
</div>
<div class="form-group">
<%= f.label :comment %>
<%= f.text_area (:comment, :class => 'form-control') %>
</div>
<div class="form-group">
<%= f.label :referral %>
<%= f.select (:referral, [['Friends', 1], ['News', 2], ['Online', 3], ['Other', 4]], :class => 'form-control')%>
</div>
<%= submit_tag ("Submit") %>
</form>
</div>
<% end %>
</body>
</html>
問題是我的表單只會在沒有:class =>“form-control”的情況下提交。 我必須在每個表單字段變量周圍加上括號。
這是我的routes.rb文件:
Rails.application.routes.draw do
root "home#index"
match ':controller(/:action(/:id))', :via => [:get, :post]
end
這是我的控制器:
class HomeController < ApplicationController
def home
render("home/index")
end
def new
@fc = FeedbackCustomer.new
end
def create
# Instantiate a new object using form parameters
@fc = FeedbackCustomer.new(feedbackcustomer_params)
if @fc.save
redirect_to(:action => "home")
end
end
def application
end
private
def feedbackcustomer_params
params.require(:fc).permit(:first_name, :last_name,
:email, :comment, :referral)
end
end
任何幫助將非常感激。 謝謝!
如果你使用Ruby 1.9+這應該工作:
<%= f.text_field :last_name, class: 'form-control' %>
如果您使用的是早於1.9的版本,則應該可以使用:
<%= f.text_field :last_name, :class => 'form-control' %>
有關此內容的更多信息,您可以使用ActionView :: Helpers :: FormTagHelper API文檔
Form Helpers Rails指南也可能對您有所幫助。
對於最新版本的Ruby,您現在也可以:
<%= f.text_field :last_name, class: 'form-control' %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.