你好有人知道如何为一个simple_form提交按钮添加一个类,用于css style.Here是我尝试做的:
_form.html.erb
<%= simple_form_for(@drink) do |f| %>
<%= f.submit "Add drink",:input_html =>{:class => "create"} %>
<%end%>
Run Code Online (Sandbox Code Playgroud)
drinks.css.scss
.create {
background-color:#2fd62f;
-moz-border-radius:4px;
-webkit-border-radius:4px;
border-radius:4px;
border:1px solid #dcdcdc;
display:inline-block;
color:#fafafa;
font-family:arial;
font-size:12px;
font-weight:bold;
padding:7px 24px;
text-decoration:none;
text-shadow:0px 0px 0px #70706f;
}.create:hover {
background-color:#2fbf0f;
}.create:active {
position:relative;
top:1px;
}
Run Code Online (Sandbox Code Playgroud) 我在我的Rails应用程序中使用简单的表单,我试图将值和类设置为特定的输入字段,如下所示
<%= f.input :email, :required => true, :autofocus => true, :value => "Email", :class => "myclass" %>
Html输出:

但我无法在实际的html表单中看到值和类集.
我究竟做错了什么?
我需要在呈现表单时向输入/ textarea/etc添加一个类,并且该字段有错误.
<input type="text" class="custom-error-class" />
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以附加到SimpleForm的CSS类列表中,但只有在字段的相应对象出错时?
我正在使用Simple_form,twitter bootstrap和rails 3.2.2
有没有人知道是否有办法在simple_form中的布尔字段上使用"切换按钮"选项进行引导?我想用按钮替换复选框.
这是我到目前为止在表单(rails)中尝试过的内容:
<%= f.input :client_approved, :input_html => { :class => 'btn btn-primary', :data => {:toggle => 'button'} } %>
Run Code Online (Sandbox Code Playgroud)
这是HTML输出:
<input class="boolean optional btn btn-primary" data-toggle="button" id="id_card_design_client_approved" name="id_card_design[client_approved]" type="checkbox" value="1">
Run Code Online (Sandbox Code Playgroud)
有关如何为simple_form输入分配按钮标签的任何想法?
我正在尝试生成一个常规的简单表单
= simple_form_for [:hq, @team, @order] do |f|
.tickets
= f.select :tickets, :aaa => "aaa"
= render "ticket"
.details
.left
= f.input :address1
= f.input :address2
= f.input :state
= f.input :country
= f.input :email
.right
= f.input :comments
Run Code Online (Sandbox Code Playgroud)
但我希望输入不用标签呈现,只是占位符中的标题"地址行1".
我知道我可以这样做,f.input :address1, :placeholder => "whatever", :label => ""但我希望它可以使用包装器配置并采用i18n值表单标签,而不是占位符.
高度以英寸为单位存储在数据库中.
但是,英尺和英寸需要以下列形式输入:
Height: [_______] feet [_______] inches
Run Code Online (Sandbox Code Playgroud)
所以我使用了虚拟属性,并使其正常工作.这是我的模型的简化版本:
class Client < ActiveRecord::Base
attr_accessible :name, :height_feet, :height_inches
before_save :combine_height
def height_feet
height.floor/12 if height
end
def height_feet=(feet)
@feet = feet
end
def height_inches
if height && height%12 != 0
height%12
end
end
def height_inches=(inches) #on save?
@inches = inches
end
def combine_height
self.height = @feet.to_d*12 + @inches.to_d #if @feet.present?
end
end
Run Code Online (Sandbox Code Playgroud)
而_form部分使用simple_form:
<%= simple_form_for(@client) do |f| %>
<ul>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.input :weight %> …Run Code Online (Sandbox Code Playgroud) 使用simple_form_for,Bootstrap和Rails 3.在一个表单中:
<%= f.input :upload, label: 'PDF file:' , input_html: {accept: ('application/pdf') } %>
我不知道我如何设置这个样式,以便"选择文件"按钮可以有不同的类('btn btn-primary').
此外,至少在使用Bootstrap时,默认情况下严重错位.见附图.
最后,当没有添加文本时,如何将文本从"未选择文件"重新定义为"选择文件",并在有文件名时显示文件名.

我正在尝试将bootstrap 3与simple_forms(来自master)集成.
现在,我有以下内容:
配置/初始化/ simple_form.rb:
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
end
config.default_wrapper = :default
config.boolean_style = :nested
config.button_class = 'btn'
end
Run Code Online (Sandbox Code Playgroud)
配置/初始化/ simple_form_bootstrap.rb:
SimpleForm.setup do |config|
config.input_class = 'form-control'
config.wrappers :bootstrap, tag: 'div', class: 'form-group', error_class: 'error' do |b|
b.use …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下模型创建一个简单的应用程序:类别 - [has_many] - >问题 - [has_many] - >答案
我有以下用于创建类别+问题的代码(categories/_form.haml.html):
= simple_form_for(@category) do |f|
= f.error_notification
= f.input :title, label: "Category title: "
= f.simple_fields_for :questions, @category.questions.build do |q|
= q.input :content, label: "Question content: "
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
我正在使用所有相同的代码来创建问题+答案(questions/_form.haml.html).我有所有关系,强大的参数,嵌套的attrs和控制器配置,它对我来说很好.
两个问题:
如何在类别/ _form.haml.html中创建多个问题?
如何一次为每个问题创建类别+多个问题+多个答案(在categories/_form.haml.html中)?
我花了几个小时试图弄清楚如何完成第二个,我能找到的所有信息都与Rails 3.0和form_for有关.他们都没有为我工作.
这里最直接的解决方案应该是这样的:
= simple_form_for(@category) do |f|
= f.error_notification
= f.input :title, label: "Category title: "
= f.simple_fields_for :questions, @category.questions.build do |q|
= q.input :content, label: "Question content: "
= q.simple_fields_for :answers, q.questions.build do |a|
= …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的应用中为货币进行自定义输入.我有那些bootstrap包装等(我认为它带有simple_form或带有bootstrap gem ...),所以,我可以做类似的事情:
<%= f.input :cost, wrapper => :append do %>
<%= content_tag :span, "$", class: "add-on" %>
<%= f.number_field :cost %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
它的工作方式与预期一致.问题是:我在很多地方需要同样的东西,而且我不想复制/粘贴它.
所以,我决定创建一个自定义输入.
到现在为止,我得到了以下代码:
class CurrencyInput < SimpleForm::Inputs::Base
def input
input_html_classes.unshift("string currency")
input_html_options[:type] ||= input_type if html5?
@builder.input attribute_name, :wrapper => :append do |b|
# content_tag(:span, "$", class: "add-on")
b.text_field(attribute_name, input_html_options)
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是我遇到了一些错误.看起来b没有像预期的那样,所以,它只是不起作用.
真的可以这样做吗?我找不到任何例子,也不能让它自己工作.
提前致谢.