【发布时间】:2013-04-14 01:36:52
【问题描述】:
在我的 Rails 应用程序中,当他查看某个帖子时,我将某个用户添加到读者列表中。
PostsController#Show
@post = Post.find(params[:id])
if (current_user)
@post.reader_links.create(reader_id: current_user.id)
end
但是,我想从客户端使用 javascript 和 ajax 请求来实现这一点。该功能将稍作更改,以便仅当用户键入特定表单(按一个键)时才会将用户添加到列表中。
所以在posts/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 请求发布。任何帮助将不胜感激。
【问题讨论】:
-
Emm... 在 .erb 文件中的 javascript 函数内?
标签: javascript ruby-on-rails ajax