2016-12-29 23 views
1

我總是收到此錯誤信息:動作控制器:異常|未定義的方法`fetch_value '的零:NilClass

NoMethodError在ListsController#新未定義的方法`fetch_value' 的零:NilClass

提取的源(左右線#8 ):

7 DEF新
8 @list = List.new
9端

我沒有得到這個錯誤的原因^^

我的routes.rb

Rails.application.routes.draw do 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
    root 'lists#index' 

    get 'home' => 'lists#index', as: 'home' 
    get 'new' => 'lists#new', as: 'new' 

    resources :lists 

end 

數據庫名稱:

:列出


型號名稱:

列表

lists_controller:

class ListsController < ApplicationController 

    def index 
     @lists = List.all 
    end 

    def new 
     @list = List.new 
    end 

    def show 
     @list = List.find(params[:id]) 
    end 

    def create 
     @list = List.new(list_params) 

     if(@list.save) 
      redirect_to @list 
     else 
      render 'new' 
     end 
    end 

    def edit 
     @list = List.find(params[:id]) 
    end 

    def update 
     @list = List.find(params[:id]) 

     if(@list.update(list_params)) 
      redirect_to @list 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @list = List.find(params[:id]) 

     @list.destroy 

     redirect_to lists_path 
    end 

    private def list_params 
     params.require(:list).permit(:date, :class, :lesson, :subject, :teacher, :room, :info) 
    end 
end 

new.html.erb

<%= form_for :list, url: lists_path do |f| %> 
    <% if @list.errors.any? %> 
     <% @list.errors.full_messages.each do |error| %> 
      <div class="alert alert-danger"><%= error %></div> 
     <% end %> 
    <% end %> 

    <div class="alert alert-info">Please fill out all the fields below.</div> 
    <p> 
     <%= f.label :date %><br> 
     <%= f.text_field :date, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :class %><br> 
     <%= f.text_area :class, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :lesson %><br> 
     <%= f.number_field :lesson, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :subject %><br> 
     <%= f.text_field :subject, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :teacher %><br> 
     <%= f.text_field :teacher, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :room %><br> 
     <%= f.text_field :room, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.label :info %><br> 
     <%= f.text_area :info, {:class => 'form-control'} %> 
    </p> 
    <p> 
     <%= f.submit({:class => 'btn btn-info'}) %> 
    </p> 
<% end %> 

使用BootstrapCDN只有默認的寶石。

感謝您的任何答案:)

+0

我不確定,但是您可以嘗試從'form'中刪除'class'屬性的標籤和文本區域,並且不應該使用已經保留的'class'類名稱。 – Deep

回答

2

在您的模型中,您有一個名爲class的屬性,它是導致問題的保留關鍵字。正如我可以看到你的代碼:

<p> 
    <%= f.label :class %><br> 
    <%= f.text_area :class, {:class => 'form-control'} %> 
</p> 

所以你不應該使用保留關鍵字作爲屬性,因爲你在這種情況下重寫它。因此,在覆蓋它時,屬性不會包含該語言所需的所有屬性。這就是你遇到錯誤的原因。

+1

謝謝:)我創建了一個新的模型:tag和:klasse而不是:date和:class,這對我來說工作正常。 – CodingSnow

1

Rails正在試圖從:list獲取的屬性應該是@list

更改form_fornew.html.erb

<%= form_for :list, url: lists_path do |f| %> 

<%= form_for @list, url: lists_path do |f| %> 
+0

感謝您的快速響應:) 我編輯了我的'new.html。erb'但我得到了同樣的錯誤:o – CodingSnow

相關問題