将 CSS 类添加到 <%= f.submit %>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5315967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:56:21  来源:igfitidea点击:

Add a CSS class to <%= f.submit %>

ruby-on-railscssruby-on-rails-3

提问by sscirrus

My question is easy:

我的问题很简单:

<%= f.submit %>

Where does the class declaration go? I'm getting errors on multiple attempts.

类声明在哪里?我多次尝试出错。

回答by Srdjan Pejic

<%= f.submit 'name of button here', :class => 'submit_class_name_here' %>

This should do. If you're getting an error, chances are that you're not supplying the name.

这个应该可以。如果您收到错误消息,很可能是您没有提供名称。

Alternatively, you can style the button without a class:

或者,您可以设置没有类的按钮样式:

form#form_id_here input[type=submit]

Try that, as well.

也试试吧。

回答by aloucas

You can add a class declaration to the submit button of a form by doing the following:

您可以通过执行以下操作向表单的提交按钮添加类声明:

<%= f.submit class: 'btn btn-default' %><-- Note: there is no comma!

<%= f.submit class: 'btn btn-default' %><-- 注意:没有逗号!

If you are altering a _form.html.erbpartial of a scaffold and you want to keep the dynamic change of the button name between controller actions, DO NOT specify a name 'name'.

如果您要更改脚手架的 _ form.html.erb部分,并且希望在控制器操作之间保持按钮名称的动态更改,请不要指定 name 'name'

Without specifying a name and depending on the action the form is rendered the button will get the .class = "btn btn-default"(Bootstrap class)(or whatever .classyou specify) with the following names:

在不指定名称的情况下,根据呈现表单的操作,按钮将获得具有以下名称的.class = "btn btn-default"(Bootstrap 类)(或.class您指定的任何内容):

  • Update model_name

  • Create model_name
    (where model_name the name of the scaffold's model)

  • 更新模型名称

  • 创建模型名称
    (其中模型名称是脚手架模型的名称)

回答by cwd

Rails 4 and Bootstrap 3 "primary" button

Rails 4 和 Bootstrap 3“主要”按钮

<%= f.submit nil, :class => 'btn btn-primary' %>

Yields something like:

产生类似:

screen-2014-01-22_02.24.26.png

screen-2014-01-22_02.24.26.png

回答by RailsZilla.com

As Srdjan Pejic says, you can use

正如 Srdjan Pejic 所说,你可以使用

<%= f.submit 'name', :class => 'button' %>

or the new syntax which would be:

或者新的语法是:

<%= f.submit 'name', class: 'button' %>

回答by BKSpurgeon

Solution When Using form_with helper

使用 form_with helper 时的解决方法

For those using Rails 5.2the withform_withhelper: don't add the comma!

对于那些使用Rails 5.2的 with form_withhelper:不要添加逗号

<%= f.submit class: 'btn btn-primary' %>

Screenshot with no comma

没有逗号的截图

HTH!

哼!

回答by benjamin.patch

By default, Rails 4 uses the 'value' attribute to control the visible button text, so to keep the markup clean I would use

默认情况下,Rails 4 使用 'value' 属性来控制可见的按钮文本,所以为了保持标记干净,我会使用

<%= f.submit :value => "Visible Button Text", :class => 'class_name' %>

回答by gsumk

both of them works <%= f.submit class: "btn btn-primary" %>and <%= f.submit "Name of Button", class: "btn btn-primary "%>

他们都工作 <%= f.submit class: "btn btn-primary" %><%= f.submit "Name of Button", class: "btn btn-primary "%>