动作控制器:例外|未定义的方法`fetch_value'为零:NilClass

时间:2016-12-29 12:29:51

标签: ruby-on-rails ruby

我总是收到此错误消息:

  

ListsController中的NoMethodError #new undefined方法`fetch_value'为零:NilClass

     

提取的来源(第8行):

     

7 def new
  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,只使用默认的宝石。

感谢您的回答:)

2 个答案:

答案 0 :(得分:2)

在您的模型中,您有一个名为class的属性,这是一个导致问题的保留关键字。我可以看到你的代码:

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

因此,您永远不应该使用保留关键字作为属性,因为在这种情况下您将覆盖它。因此,当覆盖它时,您的属性将不包含该语言所需的所有属性。这就是你收到错误的原因。

答案 1 :(得分:1)

Rails正试图从:list获取属性@list

更改 form_for 中的 new.html.erb

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

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