Ruby on rails 3 formtastic form中的select字段的默认值,无模型

Ruby on rails 3 formtastic form中的select字段的默认值,无模型,ruby-on-rails-3,formtastic,Ruby On Rails 3,Formtastic,我有一个formtastic表单来收集用于生成报告的参数。我知道formtastic设计用于模型,但我需要在这里使用它,因为表单位于activeadmin页面中 这一切都很好,但我不能为选择设置默认值。我愿意实施一个“卑鄙的黑客”来让它工作。我宁愿不要实现一个假的模型 只是在表单上设置默认值 表单代码如下所示 <%= semantic_form_for "report", :url => report_admin_payments_path do |f| %> <%=

我有一个formtastic表单来收集用于生成报告的参数。我知道formtastic设计用于模型,但我需要在这里使用它,因为表单位于activeadmin页面中

这一切都很好,但我不能为选择设置默认值。我愿意实施一个“卑鄙的黑客”来让它工作。我宁愿不要实现一个假的模型 只是在表单上设置默认值

表单代码如下所示

<%= semantic_form_for "report", :url => report_admin_payments_path do |f| %>
  <%= f.inputs do %>
    <%= f.input :report_type, :as => :select, :collection => report_types, :input_html => {:style => "width:180px"}  %>    
    <%= f.input :start_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => 1.month.ago.strftime("%Y-%m-%d")} %>
    <%= f.input :end_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => Time.zone.now.strftime("%Y-%m-%d")} %>
    <%= f.input :country, :as => :select, :collection => locales, :input_html => {:style => "width:180px"}%>
  <% end %>
  <%= f.actions :submit %>
<% end %>
report_admin_payments_path do|f|%>
:select,:collection=>report\u types,:input\u html=>{:style=>“width:180px”}%>
:string,:input_html=>{:class=>'datepicker',:style=>“width:60px”,:value=>1.month.ago.strftime(“%Y-%m-%d”)}%>
:string,:input_html=>{:class=>'datepicker',:style=>“width:60px”,:value=>Time.zone.now.strftime(“%Y-%m-%d”)}%>
:select,:collection=>locales,:input_html=>{:style=>“width:180px”}%>
提前感谢,


马特

这个或类似的东西应该能满足你的需要

class LightModel

  # Subclass this for a model that looks like an ar model but has no persistence
  # good for formtastic forms

  include ActiveModel::Naming
  include ActiveModel::Validations

  def initialize(attributes = {})
    @attributes = attributes
  end  

  # read only atm
  def method_missing(m, *args, &block)
    @attributes[m]
  end  

end
谢谢


马特

这看起来是一种巧妙的方法