2012-10-04 55 views
-1

我有一个应用程序,用户可以在其中提交项目。对于每个字段,他们都可以选择将新数据放入数据库,或者从传递的项目中选择旧数据以填充该字段。Ruby on Rails:未定义的方法`new_tech'

我无法得到这个对于这段代码在我的新观点工作:

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

任何想法?我是一个铁轨小菜,所以请试着回答时记住这一点。任何帮助都感激不尽。在此先感谢

回答

0

检查API为text_field

text_field(object_name, method, options = {}) 

的第一个参数是object_name在这里是:tech而不是:new_tech:new_tech既不是属性,也不是您正在迭代的tech对象的方法,因此它失败。

使得输入域相同的属性作为标签的一个

<%= tech.label :tech %><br /> 
<%= tech.text_field :tech, :maxlength => 30 %> 
+0

感谢您的帮助相匹配。如果用户从下拉菜单中选择了某些东西,现在不会覆盖它。我试图通过这样做来解决这个问题,'@technol.tech = params [:new_tech]除非params [:new_tech] .blank?' – Jazz

+0

如果你仍然想使用new_tech字段为用户添加一些,你可以做到这一点通过使用text_field_tag而不是tech.text_field http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag – oldergod

相关问题