2013-05-09 140 views
0

我最近添加了一個新的Model(discussion.rb)和Controller(discussions_controller.rb)。我試圖讓後評論與討論一起工作。未定義的方法`discussion_postcomments_path'爲#<#<Class:0x7a1c360>:0x7a20c38>

discussion.rb

class Discussion < ActiveRecord::Base 

    belongs_to :user 
    has_many :postcomments, dependent: :destroy 
    validates :user_id, presence: true 
    validates :content, presence: true 

    attr_accessible :content, :user_id 

    default_scope order: 'discussions.created_at DESC' 
end 

下面是我在路線

resources :discussions, :path => "disc" 

resources :users do 
    member do 
    get :following, :followers 
    end 
end 
resources :sessions, only: [:new, :create, :destroy] 
resources :microposts, only: [:create, :destroy] 
resources :discussions, only: [:create, :destroy] 
resources :relationships, only: [:create, :destroy] 
resources :microposts do 
    resources :postcomments 
    resources :discussions do 
    resources :postcomments 
    end 
end 

這裏的postcomments模型

class Postcomment < ActiveRecord::Base 


    attr_accessible :comment_content 

    belongs_to :user 
    belongs_to :micropost 
    belongs_to :discussion 

    validates :comment_content, presence: true 
    validates :user_id, presence: true 

    default_scope order: 'postcomments.created_at ASC' 
end 

我想在視圖中使用這種除,我獲得標題中發佈的錯誤

<%= form_for([discussion, @comment]) do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
    <div class="field"> 
    <%= f.text_field :comment_content %> 
    </div> 
    <div class="ItemContainer"> 
<div class="ItemInput"> 
    <button class="btn" type="submit"> 
    Comment 
    </button> 
    </div><div class="ItemCommentCount"> 
<% end %> 

它說,錯誤是來自該行

<%= form_for([discussion, @comment]) do |f| %> 

任何人都知道我如何可以解決此問題path引起的?

class PostcommentsController < ApplicationController 
def create 
    @micropost = Micropost.find(params[:micropost_id]) 
    @comment = Postcomment.new(params[:postcomment]) 
    @comment.micropost = @micropost 
    @comment.user = current_user 
    if @comment.save 
     redirect_to(:back) 
    else 
     render partial: 'shared/_postcomment_form', locals: { micropost: @micropost } 
    end 
    end 

def createdisc 
    @discussion = Discussion.find(params[:discussion_id]) 
    @comment = Postcomment.new(params[:postcomment]) 
    @comment.discussion = @discussion 
    @comment.user = current_user 
    if @comment.save 
     redirect_to(:back) 
    else 
     render partial: 'shared/_postcomment_form', locals: { discussion: @discussion} 
    end 
    end 

end 

回答

0

嘗試在您的路線文件中合併使用resources :discussions。我在下面進行了編輯,假設您不打算在微博下進行討論。

resources :discussions, only: [:create, :destroy], path: "disc" do 
    resources :postcomments 
end 
resources :users do 
    member do 
    get :following, :followers 
    end 
end 
resources :sessions, only: [:new, :create, :destroy] 
resources :microposts, only: [:create, :destroy] 
resources :relationships, only: [:create, :destroy] 
resources :microposts do 
    resources :postcomments 
end 
0

此行

<%= form_for([discussion, @comment]) do |f| %> 

discussion應該是一個實例變量:

<%= form_for([@discussion, @comment]) do |f| %> 

然後,你需要在控制器

+0

定義@discussion任何想法基於我在上面發佈的代碼是如何讓我可以獲得後評論的,從而將自己與討論聯繫起來而不是討論scussion?我正在嘗試爲BOTH使用postcomment。這裏是我得到的錯誤'找不到ID的Micropost','PostcommentsController#create'中的ActiveRecord :: RecordNotFound。我只是張貼了PostcommentsController – user2159586 2013-05-09 03:35:07

+0

我想說的是,將自己與討論聯繫在一起,而不是微博。我可以對micropost發表評論,但我似乎無法進行討論 – user2159586 2013-05-09 03:48:28

相關問題