Bob*_*Bob 16 ajax jquery ruby-on-rails ruby-on-rails-3
我的设置:Rails 3.0.9,Ruby 1.9.2,jQuery 1.6.2
我有一个表单,为用户显示多张照片和评论,我希望实现内联评论.
<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>
Run Code Online (Sandbox Code Playgroud)
我想在用户点击textarea字段中的回车键时提交一个AJAX帖子.这是我到目前为止的javascript(不完整)
$('#newsfeed').delegate('.comment_box', 'keydown', function (event){
event.preventDefault();
$.post('/sub_comments', ...);
});
Run Code Online (Sandbox Code Playgroud)
我正在使用该delegate方法,因为<div id='newsfeed'>内容可以被另一个AJAX调用替换.我需要的是jQuery post方法的语法,假设我需要传递一些形式参数,比如说photo_id等等.假设我有办法访问参数的值,那么post调用创建参数的语法是什么,Rails期望它们的方式
这是标准的Rails位
sub_comments_controller.rb
def new
@sub_comment = SubComment.new
respond_to do |format|
format.html # new.html.erb
format.js
end
end
Run Code Online (Sandbox Code Playgroud)
另外,我不想对<%= form_for(@sub_comment, :remote => true) do |f| %>我可以添加的每个内联注释使用通常的.我还看了看Ryan Bates的railscast,但代码看起来已经过时了.
cmp*_*lis 32
只要在rails端正确解释数据,您就可以设置帖子以任何方式构造数据,但最佳做法是使用具有所有值的'model-name'对象.
使用Javascript
$.ajax({
url: "/sub_comments",
type: "POST",
data: {subcomment: {
field: val,
field2: val, etc... }},
success: function(resp){ }
});
Run Code Online (Sandbox Code Playgroud)
轨道
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18495 次 |
| 最近记录: |