jQuery 发布到 Rails [英] jQuery post to Rails
问题描述
我的设置:Rails 3.0.9、Ruby 1.9.2、jQuery 1.6.2
我有一个可以为用户显示多张照片和评论的表单,我希望实现内嵌评论.
<div><div class="photo_title">2011 年夏季</div><div class="newsfeed_photo"><a href="..."/></a><textarea class="comment_box">写评论...</textarea>
<div><div class="comment_title">看电影</div><textarea class="comment_box">写评论...</textarea>
我想在用户点击 textarea 字段中的 Enter 键时提交 AJAX 帖子.这是我到目前为止所拥有的 javascript(不完整)
$('#newsfeed').delegate('.comment_box', 'keydown', function (event){event.preventDefault();$.post('/sub_comments', ...);});
我使用 delegate
方法是因为
post
方法的语法,假设我需要传递一些表单参数,比如 photo_id
等.假设我有办法访问params,post
调用以 Rails 期望的方式创建 params 的语法是什么这是标准的 Rails 位
sub_comments_controller.rb定义新@sub_comment = SubComment.newresponse_to do |格式|format.html # new.html.erb格式.js结尾结尾
我也不想使用通常的 <%= form_for(@sub_comment, :remote => true) do |f|%>
对于我可以添加的每个内联注释.我还查看了 Ryan Bates 的 railscast,但代码看起来已经过时.
你可以设置你的帖子以任何方式构造数据,只要它在 rails 端被正确解释,但最好的做法是有一个对象'model-name' 包含所有值.
Javascript
$.ajax({url: "/sub_comments",类型:POST",数据:{子评论:{字段:val,field2: val, 等等... }},成功:功能(响应){}});
导轨
def 创建@sub_comment = SubComment.new params['subcomment']如果@sub_comment.save渲染:json =>{ } # 如有必要,发回任何数据别的渲染:json =>{ }, :status =>500结尾结尾
My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2
I have a form that shows multiple photos and comments for a user and I wish to implement inline commenting.
<div id="newsfeed">
<div>
<div class="photo_title">Summer 2011</div>
<div class="newsfeed_photo">
<a href="..." /></a>
</div>
<textarea class="comment_box">Write a comment...</textarea>
</div>
<div>
<div class="comment_title">Seeing a movie</div>
<textarea class="comment_box">Write a comment...</textarea>
</div>
I want to submit an AJAX post upon the user hitting the enter key in the textarea field. Here's the javascript (incomplete) that I have so far
$('#newsfeed').delegate('.comment_box', 'keydown', function (event){
event.preventDefault();
$.post('/sub_comments', ...);
});
I'm using the delegate
method because <div id='newsfeed'>
contents could be replaced with another AJAX call. What I need is the syntax for jQuery post
method assuming I need to pass some form params like say photo_id
, etc. Assume that I have a way to access the values for the params, what's the syntax for the post
call to creating params the way Rails expect them
Here's the standard Rails bits
sub_comments_controller.rb
def new
@sub_comment = SubComment.new
respond_to do |format|
format.html # new.html.erb
format.js
end
end
Also I don't want to use the usual <%= form_for(@sub_comment, :remote => true) do |f| %>
for each and every inline comment I could add. I have also taken a look at Ryan Bates's railscast but the code looks outdated.
You can setup your post to structure the data in any way as long as it is interpreted correctly on the rails end, but best practice is to have an object of 'model-name' with all the values.
Javascript
$.ajax({
url: "/sub_comments",
type: "POST",
data: {subcomment: {
field: val,
field2: val, etc... }},
success: function(resp){ }
});
Rails
def create
@sub_comment = SubComment.new params['subcomment']
if @sub_comment.save
render :json => { } # send back any data if necessary
else
render :json => { }, :status => 500
end
end
这篇关于jQuery 发布到 Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!