我想讓用戶輸入一個項目到數據庫中。其中一個領域允許他們爲該項目輸入多種技術。Ruby on Rails:未定義的方法`model_name'爲NilClass:類
這是我的項目控制器,新建並創建動作。
def new
@project = Project.new
@all_technols = Technol.all
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
def create
@project = Project.new(params[:project])
params[:technols][:id].each do |technol|
if !technol.empty?
@project.projecttechnols.build(:technol_id => technol)
end
end
end
這是我的多選技術下拉菜單的新項目視圖。
<%= fields_for(@project_technol) do |ab| %>
<div class="tech">
<%= ab.label "All Tech" %><br/>
<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true}) %>
</div>
<% end %>
project.rb
class Project < ActiveRecord::Base
attr_accessible :tech
has_many :projecttechnols
has_many :technols, :through => :projecttechnols
end
technol.rb
class Technol < ActiveRecord::Base
attr_accessible :tech
has_many :projecttechnols
has_many :projects, :through => :projecttechnols
end
projecttechnol.rb
class Projecttechnol < ActiveRecord::Base
attr_accessible :project_id, :technol_id
belongs_to :technol
belongs_to :project
end
目前,我有一個用戶可以輸入新技術的頁面。但是我想將這個選項移到創建新項目頁面,在那裏他們可以選擇現有的技術,或者輸入一個新的項目,或者兩者兼而有之,然後他們可以保存這個項目。
但是,當我嘗試保存新項目時,出現此錯誤。
Showing /home/james/Desktop/webapp/app/views/projects/new.html.erb where line #233 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #233):
233: <%= fields_for(@project_technol) do |ab| %>
234:
235: <div class="tech">
236: <%= ab.label "All Tech" %><br/>
我是新來的鐵路,仍然在學習,所以請記住,當回答。提前致謝。編輯
改變
@project.projecttechnols.build(:technol_id => technol)
到
@project_technol = @project.projecttechnols.build(:technol_id => technol)
後
現在我得到這個錯誤:
NoMethodError in Projects#create
undefined method `map' for nil:NilClass
Extracted source (around line #240):
237: <div class="tech">
238: <%= ab.label "All Tech" %><br/>
239:
240: <%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true}) %>
241: </div>
242: <% end %>
個EDIT 2個
@all_technols = Technol.all在創建行動
現在我得到這個錯誤。
NoMethodError in Projects#show
Showing /home/james/Desktop/webapp/app/views/projects/show.html.erb where line #181 raised:
undefined method `technol' for #<Project:0xb36823c>
Extracted source (around line #181):
178: <h3>Related books</h3>
179:
180: <ul>
181: <% @project.technol.each do |technol| %>
182: <li><%= technol.tech %> <%= link_to "Details", technol_path(technol) %></li>
183: <% end %>
184: </ul>
的可能的複製[Ruby on Rails的:對於NilClass未定義的方法\'模型\ _name」:類](https://開頭stackoverflow.com/questions/12618834/ruby-on-rails-undefined-method-model-name-for-nilclassclass) –