加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby on Rails – 将CSS添加到text_field

发布时间:2020-12-17 02:55:21 所属栏目:百科 来源:网络整理
导读:我在Rails中创建了一个表单,当我向text_field添加一个CSS类时,表单将不会提交. 我还是铁杆新手,所以任何帮助都会非常感激. 这是我的表格: % @page_title = "Contact" % hr div class="col-lg-12" h1 class="page-header" BK Legal Courier Corp br /Call BK
我在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 =>的情况下提交.其中的“形式控制”.我必须在每个表单字段变量周围加上括号.

这是我的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 Documentation

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读