简体   繁体   English

Rails 4 JQuery Ajax没有显示

[英]Rails 4 JQuery Ajax not showing up

My created thing object is not showing up in the view using jQuery/Ajax. 使用jQuery / Ajax,我创建的thing对象未显示在视图中。

I want to append to the div labeled things here index.html.erb 我想在这里将div标记为index.html.erb

<h1>Listing things</h1>
<%= link_to 'New Thing', new_thing_path, id: "new_thing_link", remote: true %><br>

    <% @things.each do |thing| %>
      <div class="things">
        <%= thing.user_name %>
        <%= thing.topic %>
        <%= thing.truth %>

        <%= link_to 'Show', thing %>
        <%= link_to 'Edit', edit_thing_path(thing) %>
        <%= link_to 'Destroy', thing, method: :delete, data: { confirm: 'Are you sure?' } %><br><br>
      </div>    
    <% end %>
<br>

So in the controller I have the js formate respond to this custom template. 因此,在控制器中,我让js formate响应了此自定义模板。 The first 2 steps works as expected, and I get 200 OK from server output. 前两个步骤按预期方式工作,我从服务器输出中获得200 OK。

create.js.erb create.js.erb

$(".actions").remove(); // remove the form
$("#new_thing_link").show(); //show the new link again
$('.things').append('<%= j render @thing %>'); //insert the new task into the index template

_create.html.erb _create.html.erb

<tr id="topics">
    <td><%= thing.user_name %> |</td>
    <td><%= thing.topic %> |</td>
    <td><%= thing.truth %></td>
</tr>

things_controller.rb things_controller.rb

def create
    @thing = Thing.new(thing_params)

    respond_to do |format|
      if @thing.save
        format.html { redirect_to things_url, notice: 'your posting was added.' }
        format.js 
      else
        format.html { render :new }
        format.json { render json: @thing.errors, status: :unprocessable_entity }
      end
    end
  end

But the new div only shows up when I refresh the page. 但是新的div仅在刷新页面时显示。 It is there when I view the source without refreshing the page and the console isn't showing any errors. 当我查看源代码而不刷新页面并且控制台未显示任何错误时,它就在那里。

Here is the server output so I know the record is being created, 这是服务器的输出,所以我知道正在创建记录,

Started POST "/things" for 127.0.0.1 at 2014-08-14 21:28:43 -0700
Processing by ThingsController#create as JS
  Parameters: {"utf8"=>"✓", "thing"=>{"user_name"=>"reggie", "truth"=>"it was difficult", "topic"=>"a topic"}, "commit"=>"Create Thing"}
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "things" ("created_at", "topic", "truth", "updated_at", "user_name") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-08-15 04:28:43.501567"], ["topic", ""], ["truth", "asdf"], ["updated_at", "2014-08-15 04:28:43.501567"], ["user_name", "asdfasd"]]
   (9.0ms)  commit transaction
  Rendered things/_thing.html.erb (0.1ms)
  Rendered things/create.js.erb (2.2ms)
Completed 200 OK in 18ms (Views: 5.7ms | ActiveRecord: 9.6ms)

the view is just not updating as I expect. 该视图只是没有更新,正如我所期望的。 What am I doing wrong? 我究竟做错了什么? Is this a turbolinks issue? 这是turbolinks问题吗?

It looks like you're using it correctly. 看来您使用正确。 Try this small change, specifying your partial location (notice that your path to the partial may be wrong as I don't know how you're organizing things): 请尝试进行此小的更改,并指定您的部分位置(请注意,您通往部分的路径可能不正确,因为我不知道您是如何组织事物的):

$('.things').append("<%= j render :partial => 'things/create', :locals => {:thing => @thing} %>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM