2012-12-25 71 views
1

發現當我去http://localhost:3000/register_entries/new,我收到此錯誤:undefined method `model_name' for NilClass:ClassNilClass:Class的未定義方法`model_name'。行動「創造」不能爲

下面是我_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 %> 

這裏是我的:

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

你快把所有的控制器動作方法私有,這意味着它們不能被稱爲行動。請參閱ActionController Rails指南的Methods and Actions部分的末尾。

+2

是的,這看起來對我來說是創造性的,所以你不能在你嘗試的方式之外調用它。將私有'sort_column'和'sort_direction'移到最後可能是最簡單的解決方案。 –

+0

謝謝。在將我的頭撞在牆上後,我總是會學到一些東西,然後進入堆棧。 – webmagnets

相關問題