Ruby on Rails:NilClass的未定义方法`model_name’:Class
我试图允许用户将项目输入数据库。 其中一个字段允许他们为该项目输入多种技术。
这是我的项目控制器,新建和创建动作。
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
这是我的多选技术下拉列表的新项目视图。
true} ) %>
目前,我有一个用户可以输入新技术的页面。 但我想将此选项移至创建新项目页面,在该页面中,他们可以选择现有技术,也可以输入新技术,或同时执行这两项,并且可以使用该项目进行保存。
编辑:更改问题,以及添加模型文件
当我尝试保存新项目时,我收到此错误。
undefined method `model_name' for NilClass:Class Extracted source (around line #233): 233: 234: 235: 236:
project.rb
class Project :projecttechnols end
technol.rb
class Technol :projecttechnols end
projecttechnol.rb
class Projecttechnol < ActiveRecord::Base attr_accessible :project_id, :technol_id belongs_to :technol belongs_to :project end
EDIT2:
def new @project = Project.new @all_technols = Technol.all #@project_technol = @project.projecttechnols.build @project_technol = Projecttechnol.new respond_to do |format| format.html # new.html.erb format.json { render json: @project } end end
参考这个
更改
@project.projecttechnols.build
至
@project.technols.build
假设您有以下模型声明
project.rb
has_many :technols
technols.rb
belongs_to :project_id
Interesting Posts
LoadError:无法加载此类文件 – selenium / webdriver / phantomjs
Ruby on Rails Global ActiveRecord :: Enum
SQLite3 :: SQLException:没有这样的表:messages:SELECT“messages”。* FROM“messages”
Ruby on Rails模型。 为什么范围为1的十进制{2,1}允许小数点后的数字?
rspec –init not working /`mkd ir’:参数无效 – ./C :( Errno :: EINVAL)
ruby / rails的自然语言日期解析器
Rails ActiveRecord按连接表关联的数量排序
通过CentOS 64位在Ruby 1.9.2中安装rubygem’mysql2’时出现编译器错误
Ruby 1.9.2发生了什么?
Rails,Ruby 1.9.3p0和mysql gem