在我的Rails應用程序中,當他查看某個帖子時,我將某個用戶添加到讀者列表中。使用javascript和ajax請求從控制器實現邏輯
PostsController#顯示
@post = Post.find(params[:id])
if (current_user)
@post.reader_links.create(reader_id: current_user.id)
end
不過,我想從客戶端實現這一點,使用JavaScript和Ajax請求。該功能將稍作更改,以便只有當他將類型轉換爲某種形式(按下某個鍵)時,纔會將該用戶添加到列表中。
所以原來的代碼將下面的代碼在職位/ show.erb.html
function typeCatch(){
$(this).off("keypress",typeCatch)//remove handler
//I WANT TO ADD THE USER TO THE LIST AT THIS POINT
}
$("#field_id").on("keypress",typeCatch)
我需要一些幫助來實現JavaScript中的原始特徵,並通過Ajax請求後,應更換。任何幫助將不勝感激。
電解金屬錳... <%@ post.reader_links.create(reader_id:current_user.id)評論的鏈接% > .erb文件內部的JavaScript函數? – ted
這會有幫助嗎? http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ 或者這個? http://railscasts.com/?tag_id=11 – FluffyJack