gpt4 book ai didi

css - Ruby on Rails - 将 CSS 添加到 text_field

转载 作者:技术小花猫 更新时间:2023-10-29 12:01:33 24 4
gpt4 key购买 nike

我在 Rails 中创建了一个表单,当我将 CSS 类添加到 text_field 时,表单不会提交。

我对 Rails 还是个新手,所以非常感谢任何帮助。

这是我的表格:

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

这是我的 Controller :

    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 Documentation

Form Helpers Rails Guide也可能对您有所帮助。

关于css - Ruby on Rails - 将 CSS 添加到 text_field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30173837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com