2012-09-05 41 views
0

所以我在這裏看到了關於這個的其他文章,很多人都沒有做@post = post.new。我讀了一些在哪裏使用複數... ??未定義的方法`model_name'爲NilClass:Class - Rails應用程序

我在我的討論代碼收到此錯誤的任何方式:

型號

class Discussion < ActiveRecord::Base 
    has_many :comment 
    belongs_to :author 
    attr_accessible :author_id, :content, :title 

    validate :comment, :presence => true 
    validate :title, :presence => true 
end 

討論控制器

class DiscussionsController < ApplicationController 
    def index 
    @discussion = Discussion.new 
    @discussions = Discussion.all 
    end 

    def create 
    @discussion = Discussion.create(params[:discussion]) 
    if @discussion.save 
     redirect_to tasks_path, :flash => {:success => 'Created a new discussion'} 
    else 
     redirect_to tasks_path, :flash => {:error => 'Failed to create a discussion'} 
    end 
    end 
end 

討論表單

<%= form_for @discussion do |f| %> 

    <p><%= f.label :title %> 
    <%= f.text_field :title %></p> 

    <p><%= f.label :content %> 
    <%= f.text_area :content %></p> 

<% end %> 

討論路線

resources :discussions do 
    resources :comments 
    end 

現在據我知道我這樣做對的,因爲我有一個任務形式設立本質上是相同的方式 - 但我看了一下我的代碼小時,用Google搜索,並試圖其它例子,現在我看到這個:

undefined method `model_name' for NilClass:Class 

Extracted source (around line #1): 

1: <%= form_for @discussion do |f| %> 
2: 
3: <p><%= f.label :title %> 
4: <%= f.text_field :title %></p> 

這應該意味着我失蹤從我的控制器東西.....是它作爲asilly作爲一個拼寫錯誤? >>

+0

你從哪裏得到代碼?你可以粘貼控制器嗎? – uday

+0

你是否逐字複製粘貼你的代碼?也就是說,你可能在某個地方有錯字嗎? – Chowlett

+0

我寫了代碼,就是整個控制器。這裏的所有代碼都被複制並逐字粘貼。如果有幫助,我可以添加模型。 – TheWebs

回答

0

我相信你的問題是你試圖創建一個任務表單的討論,但只定義了討論控制器而不是任務控制器。

+0

不,不是。任務表單適用於創建,編輯,刪除。它不會呈現的討論形式。任務形式與手頭的問題無關。 – TheWebs

0

您必須添加:method =>:發佈到創建對象的表單,否則表單將通過GET請求提交。

<%= form_for @discussion , :method => :post do |f| %> 
+0

這完全沒有幫助.....其實它只是做了完全相同的事情 – TheWebs

0

難道是索引視圖 that form_for?

如果沒有,那麼你應該添加一個新行動到控制器,並在您指數行動做@discussion = Discussion.new那裏沒有。

0

如果烏拉圭回合模型關係是完全爲u已經提供,那麼他們是不正確的

class Discussion < ActiveRecord::Base 
    has_many :comment #has_many :comments 
    belongs_to :author 
    attr_accessible :author_id, :content, :title 

    validate :comment, :presence => true #valide :comments, :presence => true 
    validate :title, :presence => true 
end 
1

你有沒有試圖把這個在討論控制器?

def new 
    @discussion = Discussion.new 
end 
相關問題