Ruby on Rails:NilClass的未定义方法`model_name':Class

时间:2023-01-02 20:24:20

I am trying to allow a user to enter a project into a database. One of the fields allows them to enter multiple technologies for that project.

我试图允许用户将项目输入数据库。其中一个字段允许他们为该项目输入多种技术。

Here is my project controller, new and create action.

这是我的项目控制器,新建和创建动作。

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

Here is my new project view for the multi select technology dropdown.

这是我的多选技术下拉列表的新项目视图。

<%= 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 %>

At the moment, I have a page where the user can enter a new technology. But I want to move this option to the create new project page, where they can either select existing technologies, or enter a new one, or do both, and they would save with that project.

目前,我有一个用户可以输入新技术的页面。但我想将此选项移至创建新项目页面,在该页面中,他们可以选择现有技术,也可以输入新技术,或同时执行这两项,并且可以使用该项目进行保存。

EDIT: Change to question, plus adding of model files

编辑:更改问题,以及添加模型文件

When I try to save a new project however, I am getting this error.

当我尝试保存新项目时,我收到此错误。

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.rb

project.rb

class Project < ActiveRecord::Base

  attr_accessible :tech


  has_many :projecttechnols
  has_many :technols, :through => :projecttechnols
end

technol.rb

technol.rb

class Technol < ActiveRecord::Base
  attr_accessible :tech

has_many :projecttechnols
has_many :projects, :through => :projecttechnols
end

projecttechnol.rb

projecttechnol.rb

class Projecttechnol < ActiveRecord::Base
  attr_accessible :project_id, :technol_id

belongs_to :technol
belongs_to :project
end

EDIT2:

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

1 个解决方案

#1


1  

Ref this

参考这个

Change

更改

@project.projecttechnols.build

To

@project.technols.build

Assumption you have following model declarations

假设您有以下模型声明

project.rb

project.rb

has_many :technols

technols.rb

technols.rb

belongs_to :project_id

#1


1  

Ref this

参考这个

Change

更改

@project.projecttechnols.build

To

@project.technols.build

Assumption you have following model declarations

假设您有以下模型声明

project.rb

project.rb

has_many :technols

technols.rb

technols.rb

belongs_to :project_id