Ruby on rails 3 在Rails 3的表单中创建“从现有选择”或“创建新”

Ruby on rails 3 在Rails 3的表单中创建“从现有选择”或“创建新”,ruby-on-rails-3,forms,rails-models,Ruby On Rails 3,Forms,Rails Models,因此,我试图设置组织的名称属性,并创建一个新的组织,或者从同一表单中的以前存在的组织中进行选择 我试着跟随Ryan Bates的railscast来讨论这个话题: 我还尝试了许多来自stack的解决方案。但是,我似乎无法让它运行(而且我有一个验证无法识别我正在使用的虚拟属性) 因此,我的组织模式: class Organization < ActiveRecord::Base has_many :materials has_many :users has_and_belongs

因此,我试图设置组织的名称属性,并创建一个新的组织,或者从同一表单中的以前存在的组织中进行选择

我试着跟随Ryan Bates的railscast来讨论这个话题:

我还尝试了许多来自stack的解决方案。但是,我似乎无法让它运行(而且我有一个验证无法识别我正在使用的虚拟属性)

因此,我的组织模式:

class Organization < ActiveRecord::Base
  has_many :materials
  has_many :users
  has_and_belongs_to_many :causes
  has_and_belongs_to_many :schools, :join_table => 'organizations_schools'
####The following line has been edited ####
  attr_accessible :name, :unlogged_books_num, :id, :new_organization_name
  attr_accessor :new_organization_name
  before_validation :create_org_from_name

  validates_presence_of :name

  def self.assign_school_to_organization(org, school)
    orgschool = OrganizationsSchool.create(:organization_id=> org.id, :school_id=> school[0])
  end

 def create_org_from_name
   create_organization(:name=>new_organization_name) unless new_organization_name.blank?
 end
end
在验证或保存实例之前,这不会将名称更改为组织名称。 我还尝试将保存前的
更改为验证前的
,但没有成功

我的组织控制器(我还尝试在create中更改此设置)

最后,我知道我的表单目前正在做什么:

<%= form_for(@organization) do |f| %>
  <% if @organization.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@organization.errors.count, "error") %> prohibited this organization from being saved:</h2>

      <ul>
      <% @organization.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <% @schools = School.all %>
  <% @organizations = Organization.all %>
  <div class="field">
      <%= f.label 'Organization Name' %><br />
      <%= f.collection_select(:name, @organizations, :name, :name, :prompt=>"Existing Organization") %>
      Or Create New
      <%= f.text_field :new_organization_name %>
    </div>
    <div class="field">
      <%= f.label :unlogged_books_num %><br />
      <%= f.number_field :unlogged_books_num %>
    </div>
    <div class="field">
      <%= f.label 'School' %><br />
      <% school_id = nil %>
      <%= collection_select(:school, school_id, @schools, :id, :name) %>  
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>
====================================================编辑2============================================ 因此,如果我尝试创建一个运行以下命令的新组织,那么这就是我从rails控制台得到的结果:
organization.create(:new\u organization\u name=>“Virtual organization”,:unlocked\u books\u num=>“3”)

你需要:

before_validation :create_org_from_name


你不想在你的before_验证方法中创建。你能详细说明一下“不起作用”吗?它是否给出错误消息?选择是否有效,但新的填写名称无效?提交表单的结果是,如果输入了虚拟变量内容,则会删除这些内容,并显示一个错误,即必须有名称才能保存表单,或者如果我取消了名称验证,则不会将任何名称放入organizations表中,我的数据库中有一个空的组织名称,我怀疑验证发生在保存之前。也许在验证之前试试
:从\u name
创建\u org\u。mbrach,我已经试过了,但它仍然无法在数据库中存储正确的值。它仍然告诉我名称不能为空。我还尝试将
:create\u org\u from\u name
的函数设置为:
self.name==new\u organization\u name,除非new\u organization\u name.blank?
,它仍然表示名称不能为空,表单会删除
new\u organization\u name
字段中写入的内容,而不保存任何信息。如果在控制台或日志中查看rails服务器的详细信息,当它点击控制器中的
create
时,
new\u organization\u name
是否会显示在参数散列的转储中?
<%= form_for(@organization) do |f| %>
  <% if @organization.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@organization.errors.count, "error") %> prohibited this organization from being saved:</h2>

      <ul>
      <% @organization.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <% @schools = School.all %>
  <% @organizations = Organization.all %>
  <div class="field">
      <%= f.label 'Organization Name' %><br />
      <%= f.collection_select(:name, @organizations, :name, :name, :prompt=>"Existing Organization") %>
      Or Create New
      <%= f.text_field :new_organization_name %>
    </div>
    <div class="field">
      <%= f.label :unlogged_books_num %><br />
      <%= f.number_field :unlogged_books_num %>
    </div>
    <div class="field">
      <%= f.label 'School' %><br />
      <% school_id = nil %>
      <%= collection_select(:school, school_id, @schools, :id, :name) %>  
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>
Processing by OrganizationsController#create as HTML
  Parameters: {"utf8"=>"✓",    "authenticity_token"=>"igoefz8Rwm/RHrHLTXQnG48ygTGLydZrzP4gEJOPbF0=", "organization"=>  {"name"=>"", "new_organization_name"=>"Virtual Organization", "unlogged_books_num"=>""},   "school"=>["1"], "commit"=>"Create Organization"}
  Rendered organizations/_form.html.erb (7.1ms)
  Rendered organizations/new.html.erb within layouts/application (8.0ms)
Completed 200 OK in 17ms (Views: 12.2ms | ActiveRecord: 1.0ms)
irb(main):001:0> Organization.create(:new_organization_name=>"Virtual Organization",    :unlogged_books_num=>"3")
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
=> #<Organization id: nil, name: nil, unlogged_books_num: 3, created_at: nil, updated_at: nil>
irb(main):002:1> Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
irb(main):003:1> 
before_validation :create_org_from_name
def create_org_from_name
  self.name = new_organization_name if not new_organization_name.blank?
end