用于NilClass的未定义方法`model_name':Class。无法找到“创建”操作

时间:2012-12-25 01:10:37

标签: ruby-on-rails ruby ruby-on-rails-3

当我转到http://localhost:3000/register_entries/new时,我收到此错误:undefined method `model_name' for NilClass:Class

以下是我的_checkoutform.html.erb。如果我将此行<% @register_entry = RegisterEntry.new %>添加到控制器的开头,则表单会出现,但是当我提交时,我会收到以下错误The action 'create' could not be found for RegisterEntriesController

<%= form_for (@register_entry) do |f| %>
  <% if @register_entry.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@register_entry.errors.count, "error") %> prohibited this register from being saved:</h2>

      <ul>
      <% @register_entry.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :publisher %>
    <%= f.collection_select :publisher_id, Publisher.order(:name), :id, :name %>
  </div>
  <div class="field">
    <%= f.label :territory %>
    <%= f.collection_select :territory_id, Territory.order(:last_worked), :id, :number %>
  </div>
  <div class="field">
    <%= f.label "Checkout Date" %>
    <%= f.date_select :checkout %>
  </div>
  <!-- <div class="field">
    <%= f.label :checkin %><br />
    <%= f.date_select :checkin %>
  </div> -->
<!--   <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_field :notes %>
  </div> -->
  <div class="actions">
    <%= f.submit "Checkout Territory" %>
  </div>
<% end %>

这是我的register_entries_controller.rb

class RegisterEntriesController < ApplicationController
    # GET /RegisterEntries
    # GET /RegisterEntries.json


    before_filter :authenticate_user!

    helper_method :sort_column, :sort_direction

    private
    def sort_column
        @register_entry.column_names.include?(params[:sort]) ? params[:sort] : "checkout"
    end

    def sort_direction
        %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
    end

    def index
        authorize! :index, @user, :message => 'Not authorized'
        @register_entries = RegisterEntry.order(sort_column + ' ' + sort_direction) #pluralized @register_entry

        respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @register_entries }
        end
    end

    # GET /RegisterEntries/1
    # GET /RegisterEntries/1.json
    def show
        @register_entry = RegisterEntry.find(params[:id])
        @publishers     = Publisher.all

        respond_to do |format|
            format.html # show.html.erb
            format.json { render json: @register_entry }
        end
    end

    # GET /RegisterEntries/new
    # GET /RegisterEntries/new.json
    def new
        @register_entry = RegisterEntry.new
        authorize! :index, @user, :message => 'Not authorized'

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

    # GET /RegisterEntries/1/edit
    def edit
        authorize! :index, @user, :message => 'Not authorized'
        @register_entry = RegisterEntry.find(params[:id])
    end

    # POST /RegisterEntries
    # POST /RegisterEntries.json
    def create
        authorize! :index, @user, :message => 'Not authorized'

        @register_entry = RegisterEntry.new(params[:register_entry])

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

    # PUT /RegisterEntries/1
    # PUT /RegisterEntries/1.json
    def update
        authorize! :index, @user, :message => 'Not authorized'

        @register_entry = RegisterEntry.find(params[:id])

        respond_to do |format|
            if @register_entry.update_attributes(params[:register_entry])
        format.html { redirect_to @register_entry, notice: 'Register Entry was successfully updated.' }
                format.json { head :no_content }
            else
                format.html { render action: "edit" }
                format.json { render json: @register_entry.errors, status: :unprocessable_entity }
            end
        end
    end

    # DELETE /RegisterEntries/1
    # DELETE /RegisterEntries/1.json
    def destroy
        authorize! :index, @user, :message => 'Not authorized'

        @register_entry = RegisterEntry.find(params[:id])
        @register_entry.destroy

        respond_to do |format|
            format.html { redirect_to register_entries_url }
            format.json { head :no_content }
        end
    end
end

与此争斗了几个小时。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您正在将所有控制器操作方法设为私有,这意味着它们无法作为操作调用。请参阅ActionController Rails指南的Methods and Actions部分的结尾。