-1

我有一个应用程序,用户可以在其中提交项目。对于每个字段,他们可以选择将新数据放入数据库中,或者从通过的项目中选择旧数据来填充该字段。

在我的新视图中,我无法让它适用于这段代码:

<%= form_for(@technol) do |tech| %>
<div class="field">
    <%= tech.label :tech %><br />
    <%=  tech.text_field :new_tech, :maxlength => 30 %><br />

    OR<br />

    <%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true} ) %>
  </div>
<%end%>

这是我的新创建操作:

新的

def new
    @project = Project.new
@technol = Technol.new(params[:tech])

@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?


@project_technol = @project.projecttechnols.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
  end

创造

def create
    @all_technols = Technol.all
    @project = Project.new(params[:project])
@technol = Technol.new(params[:tech])


    @project.client = params[:new_client] unless params[:new_client].blank?
    @project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?

    @technol.tech = params[:new_tech] unless params[:new_tech].blank?

    @project.role = params[:new_role] unless params[:new_role].blank?
    @project.industry = params[:new_industry] unless params[:new_industry].blank?
    @project.business_div = params[:new_business_div] unless params[:new_business_div].blank?


    params[:technol].each_value do |tech|

    technology = Technol.find_or_create_by_tech(tech.strip)

    @project_technol = @project.projecttechnols.build(:technol_id => technology.id) 

  end

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render json: @project, status: :created, location: @project }
      else
        format.html { render action: "new" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

使用此代码,我在尝试打开创建新项目页面时收到此错误:

NoMethodError in Projects#new

line #261 raised:

undefined method `new_tech' for #<Technol id: nil, tech: nil, created_at: nil, updated_at: nil>
Extracted source (around line #261):

258: <%= form_for(@technol) do |tech| %>
259: <div class="field">
260:     <%= tech.label :tech %><br />
261:     <%=  tech.text_field :new_tech, :maxlength => 30 %><br />
262: 
263: OR<br />
264: 

有任何想法吗?我是一个 Rails 菜鸟,所以在尝试回答时请记住这一点。任何帮助都感激不尽。提前致谢

4

1 回答 1

0

检查 APItext_field

text_field(object_name, method, options = {})

第一个参数是object_namewhich here is:tech和 not :new_tech:new_tech既不是tech您正在迭代的对象的属性也不是方法,因此它会失败。

使输入字段与标签的属性匹配

<%= tech.label :tech %><br />
<%= tech.text_field :tech, :maxlength => 30 %>
于 2012-10-04T08:35:13.810 回答