我正在使用Rails 3.0,Ruby 1.9.2和Plataformatec simple_form gem.
下面的代码呈现带有使用者名称的选择框.我想让它显示一个带有消费者位置的选择框.我怎么做?
查看代码:
<%= simple_form_for @request do |f| %>
<%= f.association :consumer, :collection => Consumer.all, :prompt => "Choose a location" %>
<%= f.button :submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
型号代码:
Consumer < ActiveRecord::Base
attr_accessor :name, :location
end
Run Code Online (Sandbox Code Playgroud) 我的rails 3.1.3 app使用的是simple_form 2.0.1和twitter_bootstrap_rails 2.0.3.
除非我使用收音机收藏,否则我的所有表单元素都很好看
= f.collection_radio_buttons :my_fieldname, [['x1', 'FOO'],
['x2', 'BAR'],
['x3', 'FOOBAR'],
['', 'None']], :first, :last
Run Code Online (Sandbox Code Playgroud)
单选按钮和标签垂直显示如下:
()
FOO
()
BAR
()
FOOBAR
()
None
Run Code Online (Sandbox Code Playgroud)
而不是像这样:
() FOO () BAR () FOOBAR () None
Run Code Online (Sandbox Code Playgroud)
我将"form-horizontal"类分配给simple_form_for,并且所有其他表单元素看起来都很好.
我需要一个附加到f.collection_radio_buttons的特殊类吗?
如何在这里将“true”设置为默认值?
selected: true选项不起作用
<%= f.input :smoke, label: "Smoke", :collection => [[true, "You can"], [false, "You can't"]],
label_method: :last, value_method: :first, as: :radio_buttons, selected: true %>
Run Code Online (Sandbox Code Playgroud) 我的rails _form代码:
<%= simple_form_for @admin_event, :html => { :class => 'main-form' } do |f| %>
<%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
Cannot convert symbol to string.
Run Code Online (Sandbox Code Playgroud)
如何设置它_form以始终使用时间转换器显示此字段,以便数据库有一个完整的字段(2000-01-01 19:30:00.000000),它只会显示在表单中19:30?
有两个问题:
如何让simple_form_for与奇异资源一起使用:与用户相关联的foo_object(参见下面的代码摘录)?我得到NoMethodError:未定义的方法foo_objects_path为@foo在simple_form_for行.foo_object_path有一个有效的路径,但似乎simple_form_for是使用复数版本.我已经google'd并阅读有关奇异资源和路径问题的SO帖子,但还没有找到解决方案.
我是否需要为:foo_object创建嵌套资源,因为它与:user相关联?如果是,那么它会与Devise的用户模型冲突吗?
路线:
devise_for :users
resource :foo_object
Run Code Online (Sandbox Code Playgroud)
模型:
:user is the Devise User's model and has_one :foo_object
:foo_object belongs_to :user
Run Code Online (Sandbox Code Playgroud)
查看(haml):(更新6/27/13:更正为=来自 - )
= simple_form_for @foo do |f|
= f.input :firstname
= f.input :lastname
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
希望很清楚.谢谢.
我想建立一个自定义表单生成器中simple_form,里面加一些隐藏字段的形式,而不使用form.hidden_field.我注意到,utf8和authenticity_token隐藏字段会自动添加到每个表单.
是否有类似的机制来添加另一个自定义隐藏字段,但仅限于由我的自定义表单生成器生成的表单?
我正在尝试在 Rails 4 中制作一个应用程序。我对表单使用简单的表单,对国家/地区列表使用 country_select gem。
我有一个地址模型,其中包括这个方法:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
Run Code Online (Sandbox Code Playgroud)
当我尝试保存新地址时,出现此错误:
undefined method `translations' for "Australia":String
Run Code Online (Sandbox Code Playgroud)
谁能看出这个方法定义有什么问题?
如果我将视图更改为:
<% if @profile.addresses.any? %>
<%= @profile.addresses.first.country.titlecase %>
<% else %>
<span class="profileeditlink">
<%= link_to "Add your location", new_address_path %>
</span>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后记录显示 - 但显示为 AU 而不是澳大利亚(这是地址模型中提供的方法)。
地址表:
create_table "addresses", force: :cascade do |t|
t.string "unit"
t.string "building"
t.string "street_number"
t.string "street"
t.string "city"
t.string "region"
t.string "zip"
t.string "country"
t.boolean "main_address"
t.boolean "project_offsite"
t.string …Run Code Online (Sandbox Code Playgroud) 我一直试图让以下类适用于选择/下拉列表,但我不能!
= f.select attr, [1,2,3], class: 'form-control', include_blank: true
Run Code Online (Sandbox Code Playgroud)
我试着在这里和这里阅读rails代码.它就像它没有实现?但它适用于文本字段:
= f.search_field attr, options.merge(class: 'form-control')
Run Code Online (Sandbox Code Playgroud)
我正在使用slim和simple_form
我无法想象如何使这个简单的PHP表单起作用.我做错了什么?
<?php
$first = "firstNumber";
$second = "secondNumber";
$second * $first = "calc";
$calc = "calc";
echo("" . $_GET['$calc'] . "<br />\n");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="new.php" method="get">
<input type="text" name="firstNumber" id="first">
<input type="text" name="secondNumber" id="second">
<input style="background-color: #0094ff;" type="submit" name="calc" value="???" id="second">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)