2013-05-29 168 views
0

在我的評論視圖頁面(show.html)我有一個按鈕,點擊後,將在不同的表格中創建一個名爲Repost的行(它有一個reposts_controller和一個轉播模型)。然而,當我點擊評論節目頁面上的圖標,我得到以下錯誤:ActiveRecord :: RecordNotFound找不到註釋沒有ID

Couldn't find Comment without an ID 

這裏的RepostsController:

class RepostsController < ApplicationController 

    def create 
    @comment = Comment.find(params[:id]) 
    @repost = @comment.reposts.build(params[:comment]) 
    @repost.user = current_user 
    if @repost.save 
     #flash[:success] = "Post created!" 
     redirect_to root_url 
    else 
     render 'activities' 
    end 
    end 
end 

我知道問題是什麼,但我掙扎一個辦法。 CommentsController可輕鬆找到要在顯示頁面上顯示的評論。但是,如何將該ID從show頁面發送到RepostsController,以便它可以創建上面顯示的所需關係?這是一種奇怪的情況,因爲該按鈕是實際的評論頁面,但由於按鈕使用不同的控制器將一行寫入到不同的表格,因此按鈕無法查看/查找評論。

謝謝! -b

添加的視圖:

<%= link_to image_tag('star.png', :size => "16x16", :align => "right"), {:controller => 'reposts', :action => 'create', :id => @comment} %> 

那的link_to送我到localhost /轉播ID = 1,並沒有什麼在轉播表中創建。

+1

我們可以看到視圖代碼中發生鏈接的部分嗎?你是從視圖傳遞'@ comment'(或註釋ID)給控制器嗎? – lurker

+0

感謝您的回覆!剛剛添加了視圖代碼。我最初在一個表單中使用了一個按鈕,這就是我給出的錯誤。我嘗試了來自不同嘗試的link_to,它只是將我發送到空白頁面,並且在數據庫中沒有創建行。 – winston

+2

該鏈接的用途是什麼?是創建評論還是創建轉發? – lurker

回答

0

下面的代碼結束了工作:

class RepostsController < ApplicationController 

def index 
    @reposts = Repost.all 
end 

    def create 
    @repost= Repost.new("comment_id" => params[:comment_id],"user_id" => params[:user_id]) 
    @content = @repost.comments.content 
    if @repost.save 
     #flash[:success] = "Post created!" 
     redirect_to root_url 
    else 
     render 'activities' 
    end 

    end 
end 

<%= link_to image_tag('purple_star.png', :size => "16x16", :align => "right"), {:controller => 'reposts', :action => 'create', :comment_id => @comment, :user_id => @comment.user}, :title=> "Pass it on!", :method=>'post' %> 

感謝您的時間和幫助大家!

相關問題