所以我正在尝试设置组织的名称属性并创建一个新组织或从同一表单中选择以前存在的组织。
我试图在这里关注 Ryan Bates 关于该主题的 railscast:http ://railscasts.com/episodes/57-create-model-through-text-field
我还尝试了许多堆栈解决方案。但是,我似乎无法让它运行(而且我有一个无法识别我正在使用的虚拟属性的验证)
所以我的组织模型:
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_org_from_name
如下:
def create_org_from_name
self.name = new_organization_name
end
这不会在验证或保存实例之前将名称更改为组织名称。我也尝试过更改before_save
to before_validation
,但没有奏效
我的组织控制器(我也尝试在创建中更改它)
def create
respond_to do |format|
@organization = Organization.new(params[:organization])
@organization.name = @organization.new_organization_name unless @organization.new_organization_name.blank?
if @organization.save
@school = params[:school]
Organization.assign_school_to_organization(@organization, @school)
format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
format.json { render json: @organization, status: :created, location: @organization }
else
format.html { render action: "new" }
format.json { render json: @organization.errors, status: :unprocessable_entity }
end
end
end
最后,我知道我的表单目前正在做什么:
<%= 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)
=================================编辑2================ ============================所以如果我尝试创建一个运行此命令的新组织,这就是我从 Rails 控制台得到的结果:Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
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>
如果 的函数create_org_from_name
是self.name = new_organization_name
,那么来自控制台的相同命令的结果为空白:
irb(main):002:1> Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
irb(main):003:1>