2012-10-04 138 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

相關問題