rails 3 + jquery帖子失败了吗?

时间:2012-01-02 12:42:22

标签: jquery ruby-on-rails ajax ruby-on-rails-3 post

好的我有一个rails 3应用程序,当我尝试对rails操作执行$ .post请求时,我会收到提示重新登录...这可能与authenticity_token或rails 3处理帖子或远程=的方式有关>真的...但说实话,我真的不知道为什么它不起作用......这是我的代码

$('#pling').click(function(e){
    e.preventDefault();
    var location = $(this).closest("form").attr("action");
    $.post(location, { note: $("#note").val() }, function(data) {
        var length = data.length;
        var div_string = "";
        for (i=0;i<length;i++)
        {
            div_string += "<p style=\"font-size: 11px; padding: 0 5px;\">"+ data[i].comment.comment +"</p>";
        }
        $("#pling").html(div_string);
    });
    $("#pay").val("");
    $("#pling").animate( { backgroundColor: "#D3ECF4" }, 1 ).animate( { backgroundColor: "#ffffff" }, 3000 );
});

这是我的rails动作

def update_note
  @user = Contact.find(params[:id])
  note = params[:note].to_s.strip
  note = "#{Time.now} - #{note.to_s} - #{current_user.name} (#{current_user.id})"
  @user.notes.create!(:note => note, :user_id => current_user.id)
  respond_to do |format|
    format.json { render :json => @user.notes}
  end
end

知道我做错了什么

如果我查看来源

,这是我的表格
<form method="post" action="/users/100/update_note?remote=true" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="yPtCzfaI5HOSWeW8HBGLthbpUmgsfgsdfgsdW4aunGDtacva6Kx0=" name="authenticity_token"></div>

1 个答案:

答案 0 :(得分:0)

而不是直接通过jquery发布帖子,你应该使用rails的ujs助手:

form_for @user, :remote => true

然后将自己插入到此处描述的表单生命周期方法http://www.alfajango.com/blog/rails-3-remote-links-and-forms/