0

我有以下代码来自我的 passport_visas.rb 模型

ActiveAdmin.register PassportVisa do

  menu :label => "Visas"
  form :partial => "form"

  index :title => "Visas"
end

这是我的部分“表单”代码

<%= semantic_form_for [:admin, @passport_visa] do |f| %>

  <%= f.inputs "Main information" do %>
            <%= f.input :country %>
            <%= f.input :citizenship, :as => :radio, :collection => {"US Citizen" => 0, "Foreign National" => 1} %>
            <%= f.input :visa_type, :as => :select, :collection => ["Tourist", "Business", "Official"] %>
            <%= f.input :visa_required, :label => "Is Visa Required?", :as => :radio, :collection => {"Required" => 0, "Not Required" => 1} %>
            <%= f.input :maximum_stay, :label => "Maximum Stay" %>
  <% end %>

  <fieldset class="actions">
    <ol>
        <li class="action input_action" id="passport_visa_add_new_entry">
            <input name="new_entry" type="button" value="Add New Entry">
        </li>
        <li class="action input_action" id="passport_visa_remove_entry">
            <input name="remove_entry" type="button" value="Remove Entry">
        </li>
    </ol>
  </fieldset>

  <!-- Problem -->
  <%= f.inputs "Entries" do %>
    <%= f.has_many :visa_entries do |entry| %>
            <%= entry.input :type_of_entry, :as => :select, :collection => ["Testing"] %>
        <% end %>
    <% end %>

  <%= f.actions %>

<% end %>

我收到此错误消息:“#Formtastic::FormBuilder:0x10c234c38 的未定义方法 `has_many'”

我整天都在尝试设置这个表单,这是我的模型代码:

护照签证.rb

class PassportVisa < ActiveRecord::Base
  has_many :visa_entries
  accepts_nested_attributes_for :visa_entries
end

visa_entry.rb

class VisaEntry < ActiveRecord::Base
  belongs_to :passport_visa
  attr_accessible :type_of_entry, :maximum_validity, :embassy_fees, :service_fees, :processing_time
end
4

1 回答 1

0

最后我找到了解决方法。所以这就是我为使它工作所做的事情:

  1. 首先,我摆脱了 _form 部分,因为由于某种原因,“has_many”在那里不起作用。
  2. 我修改了我的 2 个模型:

护照签证.rb

class PassportVisa < ActiveRecord::Base
  has_many :visa_entries
  accepts_nested_attributes_for :visa_entries
  attr_accessible :visa_entries_attributes, :country, :citizenship, :visa_type, :visa_required, :maximum_stay
  validates_presence_of :country, :citizenship, :visa_type, :visa_required, :maximum_stay
end

visa_entry.rb

class VisaEntry < ActiveRecord::Base
  belongs_to :passport_visa
  attr_accessible :type_of_entry
  validates_presence_of :type_of_entry
end

最后一部分,实际形式:

form do |f|

        f.inputs "Entries" do
            f.has_many :visa_entries do |ff|
                ff.input :type_of_entry, :as => :select, :collection => ["Testing"]
            end
        end

        f.actions

    end

再见!

于 2013-05-08T14:39:33.427 回答