這裏是發生了什麼事情:
用寧靜的路線,你已經設置了報告後操作。這似乎是合理的,因爲報告正在執行創建操作。
不幸link_to不知道甚至不在乎。一般情況下,鏈接只執行獲取請求。表格會產生髮布請求,但在這種情況下他們似乎沒有必要。
您有四個選項。
使[! ]鏈接提交到報表的表單上的按鈕。
打破RESTful準則並重新定義報告接收穫取請求。
將此設置爲link_to_remote調用。注:這依賴於JavaScript,如果Javascript被禁用,將不會工作。
將方法選項添加到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 %>
<(%)=的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
當加入他們必須在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