因此,我正在尝试设置组织的名称属性并创建新组织,或者从同一表单中选择以前存在的组织。
我试图按照Ryan Bates关于这个话题的轨道广播: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
更改为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 %>
================================== EDIT ============ ================================ 所以,目前,当我尝试使用仅在虚拟文本字段中编写的内容创建组织时,我的日志告诉我以下内容:
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>
答案 0 :(得分:0)
你需要:
before_validation :create_org_from_name
和
def create_org_from_name
self.name = new_organization_name if not new_organization_name.blank?
end
您不希望在before_validation方法中进行创建。