Javascript Rails 4在表单提交后不呈现ajax

Javascript Rails 4在表单提交后不呈现ajax,javascript,jquery,ajax,ruby-on-rails-4,Javascript,Jquery,Ajax,Ruby On Rails 4,js工作的一部分,只有当我在浏览器上点击“刷新”按钮时,才会进行重新渲染,因此,在db中发送和创建数据,但js不会渲染新数据 我的JS: $('#myform').submit(function() { var valuesToSubmit = $(this).serialize(); $(this).find('textarea').addClass('uneditable-input').attr('disabled', 'disabled'); $.aja

js工作的一部分,只有当我在浏览器上点击“刷新”按钮时,才会进行重新渲染,因此,在db中发送和创建数据,但js不会渲染新数据

我的JS:

 $('#myform').submit(function() {  
    var valuesToSubmit = $(this).serialize();
      $(this).find('textarea').addClass('uneditable-input').attr('disabled', 'disabled');
    $.ajax({
        url: $(this).attr('action'), //sumbits it to the given url of the form
        data: valuesToSubmit,
        type: "POST",
        dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
    }).complete(function(json){
        //act on result.
      //alert("submitted");


     $("#disabletext").removeClass('uneditable-input').removeAttr('disabled', 'disabled').val('');
        #this line below is the only thing that doesn't work
       $('#indexstream').html("<%= escape_javascript (render 'postline') %>");

      #this line works, but it hides the most recent post in the index and shows it again
      #but the most recent post should be the one after this form submits
      $("#userlink").hide().show("slow").effect('highlight', {}, 3000);




    });   
    return false; // prevents normal behaviour
});
});
$('#myform').submit(函数(){
var valuesToSubmit=$(this).serialize();
$(this).find('textarea').addClass('uneditable-input').attr('disabled','disabled');
$.ajax({
url:$(this.attr('action'),//将其汇总到表单的给定url
数据:价值待提交,
类型:“POST”,
数据类型:“JSON”//您希望在普通调用和ajax调用之间有所不同,JSON是标准的
}).complete(函数(json){
//根据结果采取行动。
//警报(“已提交”);
$(“#disabletext”).removeClass('uneditable-input').removeAttr('disabled','disabled').val('');
#下面这一行是唯一不起作用的
$('#indexstream').html(“”);
#这一行可以工作,但它隐藏了索引中最近的文章,并再次显示
#但是最近的帖子应该是在这个表单提交之后的帖子
$(“#userlink”).hide().show(“slow”).effect('highlight',{},3000);
});   
return false;//防止正常行为
});
});
视图:


_postline.html.erb

<% @posts.each do |post| %>

            <div id="streamline"> 

            <div id ="userlink">

              <%= image_tag post.avatar.url(:small), :style => "border-radius: 5px; border: 1px solid #e1e8ed;" %> 
           <%= post.firstname %> <%= " "  %>  <%= post.lastname %><%= " " %>  <%= link_to post.email, user_path(post.user_id), {class: "usernamelink", style: "font-weight: normal;"} %>

  <abbr class="timeago" title="<%= post.created_at.getutc.iso8601 %>" style="float: right; margin-right: 5px; font-size:90%;
                                                                             margin-top: 12px; color: #8899a6; text-decoration: none !important;">
    <%= post.created_at.to_s %>
</abbr>



            <br /><br />
              <% unless post.description == "status" %>
               <div style="background: #EF4836; width: 10px; height: 5px; float: right; margin-top: -58px; text-align: center; border-radius: 10px; color: #fff; padding-top: 2px; padding-bottom: 2px; margin-right: 5px;">
              </div>
            <h1 style="style= z-index: 100000; color: #3399FF "><%= link_to post.title, post_path(post), {class: "usernamelink", style: "color: #3399FF;"} %></h1>


               <% if post.asset1.file? %>
            <%= image_tag post.asset1.url(:small) %><% end %> <% if post.asset2.file? %><%= image_tag post.asset2.url(:small) %><% end %> <% if post.asset3.file? %><%= image_tag post.asset3.url(:small) %><% end %> <% if post.asset4.file? %><%= image_tag post.asset4.url(:small) %>
            <% end %>
              <br /><br />
    <i class="fa fa-map-marker fa-lg" style="margin-right: 5px; margin-left: 5px;"></i>  <%= post.school %>  <div style="float: right; text-align: left; margin-right: 20px;"> <i class="fa fa-usd "></i><%= post.price %></div>

              <% end %>
              <% if post.description == "status" %>
              <div style="background: #2ECC71; width: 10px; height: 5px; float: right; margin-top: -58px; text-align: center; border-radius: 10px; color: #fff; padding-top: 2px; padding-bottom: 2px; margin-right: 5px;">

              </div>
              <h1><%= auto_link( post.title ) %></h1>


                    <% if post.asset1.file? %>
            <%= image_tag post.asset1.url(:small) %><% end %> <% if post.asset2.file? %><%= image_tag post.asset2.url(:small) %><% end %> <% if post.asset3.file? %><%= image_tag post.asset3.url(:small) %><% end %> <% if post.asset4.file? %><%= image_tag post.asset4.url(:small) %>

            <% end %>
             <br /><br />

              <% end %>






           </div>
         </div>  

<% end %>

“边框半径:5px;边框:1px实心ᜯe1e8d;“%”>






发布控制器创建方法:

def create

    @post = Post.new(post_params) 
    @post.email = current_user.username
    @post.user_id = current_user.id
    @post.firstname = current_user.firstname
    @post.lastname = current_user.lastname
    @post.avatar = current_user.avatar
    @post.school = current_user.school


    respond_to do |format|  
      if @post.save
        format.html { redirect_to posts_path, :notice => '<i class="fa fa-check fa-5x"></i>'.html_safe }
       # format.js   # @current_item = @post 
        format.json { render action: 'index', status: :created, location: @post }
        #format.html {render 'postline'}


      else
        format.html { render action: 'new' }
       format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end

  end
def创建
@post=post.new(post_参数)
@post.email=当前用户用户名
@post.user\u id=当前用户id
@post.firstname=当前_user.firstname
@post.lastname=当前用户.lastname
@post.avatar=当前用户.avatar
@post.school=当前用户学校
回应做什么|格式|
如果@post.save
format.html{redirect_to posts_path,:notice=>''.html_safe}
#format.js#@current_item=@post
format.json{render action:'index',status::created,location:@post}
#format.html{render'postline'}
其他的
format.html{呈现操作:'new'}
format.json{render json:@post.errors,status::unprocessable_entity}
结束
结束
结束

代码在我看来很好,我似乎根本无法让它工作。我需要你们大家,在ajax中已经为此工作了好几天,却没有任何结果,
成功:
元素在哪里?因此,您可以用您的ajax o/p.oh替换html内容,这样.html就不能在ajax中工作。完成?将
complete
更改为
success
无更改在ajax中我们有两个事件
成功:
错误:
。一旦您提交了ajax,响应应该会再次出现在我们的html页面上,对吗。。例如,我们可以在
suces:
(关于ajax)中提到,用ajax响应替换html页面中的这一特定内容。如果出现错误,那么示例
alert(“出错”)
您能否尝试用代码回答,以便我了解您的意思?
def create

    @post = Post.new(post_params) 
    @post.email = current_user.username
    @post.user_id = current_user.id
    @post.firstname = current_user.firstname
    @post.lastname = current_user.lastname
    @post.avatar = current_user.avatar
    @post.school = current_user.school


    respond_to do |format|  
      if @post.save
        format.html { redirect_to posts_path, :notice => '<i class="fa fa-check fa-5x"></i>'.html_safe }
       # format.js   # @current_item = @post 
        format.json { render action: 'index', status: :created, location: @post }
        #format.html {render 'postline'}


      else
        format.html { render action: 'new' }
       format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end

  end