我一直在使用formtastic来在rails应用程序上生成HTML表单.然而,我的问题实际上与HTML有关.
今天我在formtastic生成复选框(:booleanformtastic lingo上的类型字段)的路上发现了一个奇怪的行为.
其余字段(非复选框)以这种方式生成:
<li>
<label for="my_textbox_field">My TextBox</label>
<input id="my_textbox_field" type="text" ... >
</li>
Run Code Online (Sandbox Code Playgroud)
但是,复选框<label>完全包含在其标签内- 如下所示:
<li>
<label for="my_boolean_field">
<input id="my_boolean_field" type="checkbox" ... >
This is my boolean field
</label>
</li>
Run Code Online (Sandbox Code Playgroud)
Formtastic哲学似乎是基于Learning to Love Forms的演示.实际上,在该演示文稿的幻灯片36上,建议使用该结构用于复选框.我想在演示文稿本身中,演示者解释了为什么这样做了,但它没有写在幻灯片上.
任何人都可以告诉我为什么在他们的<label>标签中包含复选框可能是一个好主意,而不是将它们放在外面,就像文本框一样?
所以我正在制作一个调查应用程序.用户在后端选择一种表单,并在前端显示为某种类型.当然,这只是理想情况.现在发生的事情是我无法访问formtastic构建表单的对象.我该怎么说"question.kind"?这只是一个错误.这是我到目前为止所拥有的......
= semantic_form_for @survey, :url => "#", :html => { :method => "get" } do |f|
- for question in @survey.questions
= user_facing_question(f)
Run Code Online (Sandbox Code Playgroud)
到目前为止,补充辅助方法是这样的:
def user_facing_question(f)
f.inputs
end
Run Code Online (Sandbox Code Playgroud) 我正在使用ActiveAdmin和Formtastic.
我有一张发票表单,上面有一个下载菜单.
form do |f|
f.inputs "Shipment Details" do
f.input :shipment_id, :label => "Shipment", :as => :select, :collection => Shipment.find(invoiceless_shipments, :order => "file_number", :select => "id, file_number").map{|v| [v.file_number, v.id] }
f.input :issued_at, :label => "Date", :as => :datepicker
... more fields ...
end
Run Code Online (Sandbox Code Playgroud)
如果表单是新发票表单,我只想显示货件的选择菜单.
如果表单是编辑表单,我不想显示发货下拉选择菜单.因此,如果表单是编辑表单,则不会更改.
我在考虑做类似的事情
if params[:action] != 'edit'
f.input :shipment_id, :label => "Shipment", :as => :select...
end
Run Code Online (Sandbox Code Playgroud)
但我收到DSL错误.
我在ActiveAdmin中为这些模型设置了一个嵌套表单(a:class_section has_many:class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
和
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Run Code Online (Sandbox Code Playgroud)
当我看到表格时,一切似乎都在正确的位置.但是,当我更新class_date时,它不会保存.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: …Run Code Online (Sandbox Code Playgroud) 好吧,我有一个与on-to-many assoc相关的两个模型.
#models/outline.rb
class Outline < ActiveRecord::Base
has_many :documents
end
#models/document.rb
class Document < ActiveRecord::Base
belongs_to :outline
end
#admin/outlines.rb
ActiveAdmin.register Outline do
form do |f|
f.inputs "Details" do
f.input :name, :required => true
f.input :pages, :required => true
...
f.buttons
end
f.inputs "Document Versions" do
f.has_many :documents, :name => "Document Versions" do |d|
d.input :file, :as => :file
d.buttons do
d.commit_button :title => "Add new Document Version"
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
正如您在admin/outlines.rb中看到的那样,我已经尝试在has_many:documents中设置:name,在commit_button中设置:title,但这两个选项都不起作用,我也试过:legend,: title,和:label,而不是:.has_many中的名称.不工作.
这是该代码的结果: 截图
我要显示的是"文档版本"而不是"文档","添加新文档版本"而不是"添加新文档"
如果有人能有解决方案,那就太好了
我搜索并尝试了很多,但我无法按照我的意愿完成它.所以这是我的问题.
class Moving < ActiveRecord::Base
has_many :movingresources, :dependent => :destroy
has_many :resources, :through => :movingresources
end
class Movingresource < ActiveRecord::Base
belongs_to :moving
belongs_to :resource
end
class Resource < ActiveRecord::Base
has_many :movingresources
has_many :movings, :through => :movingresources
end
Run Code Online (Sandbox Code Playgroud)
Movingresources包含其他字段,例如quantity.我们正在研究"账单"的观点.感谢formtastic通过写作简化整个关系的事情
<%= form.input :workers, :as => :check_boxes %>
Run Code Online (Sandbox Code Playgroud)
我得到一个真正漂亮的复选框列表.但到目前为止我还没有发现的是:我如何使用'movingresource'中的附加字段,在每个复选框的下一个或每个复选框下使用该模型中的所需字段?
我看到了不同的方法,主要是手动循环一个对象数组并创建适当的表单,使用:for in form.inputs part,or.但是这些解决方案都不是干净的(例如,用于编辑视图但不适用于新的,因为没有构建或生成所需的对象并且生成它们导致混乱).
我想知道你的解决方案!
我现在已经使用Formtastic一段时间了,这对于加快表单的实现非常有用.但是,我有一个特殊情况,我需要在我的表单中显示更多的自定义.具体来说,该字段是用于上传图像的文件上载表单,并且在编辑表单上,我想显示已上载的图像的当前版本的缩略图.

我有这个工作,但它要求我使用自定义HTML标记,这意味着任何时候Formtastic更改输出格式,我需要更新我匹配的HTML.这就是我现在所拥有的:
<%= form.inputs do %>
<% if form.object.new_record? -%>
<%= form.input :image, :required => true, :hint => 'Maximum size of 3MB. JPG, GIF, PNG.' %>
<% else -%>
<li class="file input required" id="profile_image_input">
<label class="label" for="profile_image">Image</label>
<%= image_tag form.object.image.url(:thumb), :class => 'attachment' %>
<%= form.file_field :image %>
<p class="inline-hints">Maximum size of 3MB. JPG, GIF, PNG.</p>
</li>
<% end -%>
<% end %>
Run Code Online (Sandbox Code Playgroud)
理想情况下,做一些更像下面的事情会更好,input_html假设是输入,提示等生成的HTML:
<%= form.inputs do %>
<%= form.input :image, :required => true, :hint => 'Maximum size …Run Code Online (Sandbox Code Playgroud) 我对表格有疑问.我有一个相当标准的表单,用标题和正文保存一个帖子(在我的应用程序中称为eReport).该表还有一个"已发布"字段,它是布尔值.保存的eReport仅在公共站点上显示此字段是否设置为true,false为默认值.
我希望在表单末尾显示两个按钮,而不是默认复选框:"立即发布"按钮和"另存为草稿"按钮.如果用户按下前者,则已发布的字段将设置为true.如果是后者,则假.在PHP中,我曾经显示2个具有不同名称值的提交字段,然后使用if/else语句处理输入以确定要构建的正确SQL查询.在Rails中,我假设我会在适当的操作下将此逻辑放在控制器中,但我不确定如何操作按钮的名称或id值.
为了记录,我正在使用Formtastic,但如果有人可以告诉我如何使用默认的Rails表单标签执行此操作,那也没关系.这是我现在的表格的代码:
<% semantic_form_for @ereport do |form| %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
<% form.buttons do %>
<%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
我有一个输入,我渲染:as =>:选择.我希望选择一个选项作为默认选项.我该怎么办呢?
谢谢,亨德里克
对于订阅表单,我需要有一个未映射到我的User对象的字段.
此字段仅用于向用户询问邀请代码,该代码是根据控制器的创建操作中的邀请列表进行检查,但不保存在任何位置,并且与此User对象无关.
我试过了 :
<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.input :invitation_key, :input_html => {:name => 'invitation_key', :id => 'invitation_key'} %>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
使用Formtastic进行此操作的正确方法是什么?