javascript - Rails 4 JQuery Ajax 未显示

标签 javascript jquery ruby-on-rails ajax

我创建的 thing 对象没有显示在使用 jQuery/Ajax 的 View 中。

我想在此处附加到 div 标记的内容 索引.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>

因此,在 Controller 中,我让 js 格式响应此自定义模板。前 2 个步骤按预期工作,我从服务器输出中得到 200 OK。

创建.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

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

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

但是新的 div 仅在我刷新页面时才会显示。当我在不刷新页面的情况下查看源代码并且控制台没有显示任何错误时,它就在那里。

这是服务器输出,因此我知道正在创建记录,

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)

View 没有按我的预期更新。我究竟做错了什么?这是涡轮链接问题吗?

最佳答案

看起来您的使用方式正确。尝试这个小更改,指定您的部分位置(请注意,您的部分路径可能是错误的,因为我不知道您如何组织事物):

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

关于javascript - Rails 4 JQuery Ajax 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25321282/

相关文章:

javascript 异步 sweetAlert

ruby-on-rails - 无法使用 mongoid 和 MongoDB 将新记录保存到我的 Rails 模型中

javascript - jQuery .animate 命令问题

javascript - 鼠标悬停图像保持不变

javascript - 如何将 .editable() 应用于动态添加的元素

ruby-on-rails - 应用程序服务器 worker 是否生成线程?

ruby-on-rails - "rails s"使用 Ruby 1.8.7 而不是 Ruby 1.9.3 (OS X 10.7.2)

javascript - 如何在 AngularJS 循环中调用第二个顺序 Promise 设计模式方法?

javascript - fabric.js fabric.Image.fromURL 在使用外部 url 时抛出错误

javascript - 从表单获取文件而不查询输入字段