そのため、組織の名前属性を設定して新しい組織を作成するか、同じフォームから既存の組織から選択しようとしています。
ここでトピックに関する 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 %>
==================================編集=============== =================================ログは次のことを教えてくれます:
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>