2009-11-11 27 views
0

我的應用程序出現問題。行動問題鏈接

我有表報告,有2列,USER_ID和COMMENT_ID 我創建鏈接文章評論觀點

<%= link_to "[ ! ]", report_comment_url(comment) %> 

class CommentsController < ApplicationController 

    def report 
    @comment = Comment.find(params[:id]) 
    @comment = CommentReport.new(params[:comment_report, :comment_id]) 
    if @comment_report.save 
     redirect_to :back 
    end 
    redirect_to :back 
    end 
end 

,但它是錯誤

的ActionController :: MethodNotAllowed

只允許發佈請求。

你有什麼建議如何發佈current_user id和comment_id到報表嗎?

回答

0

鑑於我假設你正在努力完成,我建議你使用link_to_remote。

+0

<(%)=的link_to 「[!]」,report_comment_url(註釋),:COMMENT_ID => comment.id,:USER_ID => comment.user_id%> 我曾嘗試這個 DEF報告 @comment =意見。找到(PARAMS [:編號]) @comment_report = CommentReport.new(PARAMS [:comment_report]) 如果@ comment_report.save redirect_to的:回 其他 redirect_to的:回 結束 結束 我是嘗試該代碼。但結果是 ID \t USER_ID \t COMMENT_ID NULL \t NULL 我如何獲得PARAMS USER_ID和COMMENT_ID – 2009-11-11 08:47:19

+0

當加入他們必須在URL幫手去參數,額外參數的link_to被視爲HTML屬性。你要這樣做:'<%= link_to「[!]」,report_comment_url(comment,:comment_report => {:comment_id => comment.id,:user_id => comment.user_id})%> – EmFi 2009-11-11 20:22:54

0

這裏是發生了什麼事情:

用寧靜的路線,你已經設置了報告後操作。這似乎是合理的,因爲報告正在執行創建操作。

不幸link_to不知道甚至不在乎。一般情況下,鏈接只執行獲取請求。表格會產生髮布請求,但在這種情況下他們似乎沒有必要。

您有四個選項。

  1. 使[! ]鏈接提交到報表的表單上的按鈕。

  2. 打破RESTful準則並重新定義報告接收穫取請求。

  3. 將此設置爲link_to_remote調用。注:這依賴於JavaScript,如果Javascript被禁用,將不會工作。

  4. 將方法選項添加到link_to調用。注:這也依賴於JavaScript,如果JavaScript被禁用,它將回退到獲取請求。

    <%= link_to "[ ! ]", report_comment_url(comment), :method => :post %> 
    

但是這些解決方案將解決所有的問題。您發佈的代碼有幾處錯誤,您可能尚未實現。

第一:

@comment = CommentReport.new(params[:comment_report, :comment_id]) 

是語法錯誤和失敗。有很多方法可以解決這個問題,首選的方法是將comment_id:roll into:rollback [:comment_report] hash來解決這個問題。

即通過PARAMS爲:

params = { 
    :id => 4, # done by report_comment_url 
    :comment_report => { 
    :attribute1 => value1, 
    ... 
    :comment_id => 4 
    } 
} 

現在你可以使用

@comment = CommentReport.new(params[:comment_report]) 

了預期的效果。

第二個: report_comment_url沒有傳遞附加參數,所以你的控制器會嘗試保存一個空記錄。將comment_report添加到report_comment_url的參數將解決此問題。

這將在註釋控制器中執行請求報告操作的遠程調用,並且需要使用參數哈希來修復其他問題。

<%= link_to_remote "[ ! ]", report_comment_url(comment, 
    :comment_report => {:attribute1 => value1, ..., :comment_id => comment.id}), 
    :method => :post %> 
0

params [:comment_report]是nil,因爲在你的link_to語句中沒有引用。既然你在註釋中提到,您的看法是:

<%= link_to "[ ! ]", report_comment_url(comment), :comment_id => comment.id, :user_id => comment.user_id %> 

然後,你需要這個在你的控制器:

@comment_report = CommentReport.new(:user_id => params[:user_id], :comment_id => params[:comment_id]) 

但我NSD同意link_to_remote將更好地爲你想要完成的任務(這是創建一個新的記錄並將用戶返回到該頁面)。您還可以避免使用@comment = Comment.find(params [:id])語句。