所以我試圖設置組織的名稱屬性,並創建一個新的組織或從以前存在的組織從同一表單中選擇。創建一個選擇從現有或創建新的形式Rails 3
我試圖跟蹤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
到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 %>
==================================編輯==== ======================================== 所以目前,當我嘗試做一個如此組織mething只寫在虛擬文本字段,我的日誌告訴我下面的:
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 =========================================== = 所以這是我從鐵軌控制檯中看到,如果我嘗試創建一個新的組織運行此命令: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>
你能說說「不行」嗎?它是否提供錯誤信息?選擇工作,但不是新的填寫名稱? – lurker
提交表單的結果將是刪除輸入的虛擬變量內容,並在保存表單之前顯示必須有名稱的錯誤,或者如果我取消了名稱驗證,則不會放入任何名稱到組織表中,我堅持在我的數據庫中的空組織名稱 – CimmerianMuse
我懷疑驗證發生在'before_save'之前。也許嘗試'before_validation:create_org_from_name'。 – lurker