无法使用js Rails追加部分

时间:2018-05-07 08:08:11

标签: javascript ruby-on-rails ajax

我试图在主窗体中附加colordata partial但无法这样做。 这是我的控制器

def colordata
    request.POST.each do |key, value|
      @color = value
    end
    @lotdetail= Master::Yarn.where('color like?', @color)
    @lotdetail.each do |v|
      puts "value= #{v[:Lotnumber]}"
    end
    respond_to do |format|
      format.js
    end
  end

这是我的js文件,它出现在名为colordata.js.erb的视图中

  $(".lot").append(<%= escape_javascript render(partial: 'colordata') %>);

这是我必须附加的主要表单

<%= form_with(model: order, remote: true, class: 'orderform') do |form| %>

  <div class="field">
    <%= form.label :design %>
    <%= form.collection_select(:design, Design.all,:Design_name,:Design_name,id: :order_design,prompt: "Choose the design")%>
  </div>

  <div class= "field">
    <%= form.label :color %>
    <%= form.collection_select(:color, Master::Color.all,:colorname,:colorname, prompt: "choose the color",class: "colors")%>
  </div>

  <div class="field">
    <%= form.label :quantity %>
    <%= form.number_field :quantity, id: :order_quantity %>
  </div>

  <div class="lot">

  </div>

  <div class= "field">
    <%= form.label :consumption %>
    <%= form.text_field :consumption, id: :order_consumption%>
  </div>

这是终端的输出

Started POST "/orders/colordata" for 127.0.0.1 at 2018-05-07 19:04:56 +0530
Processing by OrdersController#colordata as */*
  Parameters: {"color"=>"Red"}
  Master::Yarn Load (0.4ms)  SELECT `master_yarns`.* FROM `master_yarns` WHERE (color like'Red')
value= 8406
  Rendering orders/colordata.js.erb
  CACHE Master::Yarn Load (0.0ms)  SELECT `master_yarns`.* FROM `master_yarns` WHERE (color like'Red')
  Rendered orders/_colordata.html.erb (1.5ms)
  Rendered orders/colordata.js.erb (2.4ms)
Completed 200 OK in 17ms (Views: 4.1ms | ActiveRecord: 2.7ms)

这是我的部分'colordata'

<%= fields_for :lotnumber do |form|%>
<%= form.label :lotnumber %>
<%= @lotdetail.each do |l|%>
<%= l[:Lotnumber]%>
<%end%>
<%= form.collection_select(:lotnumber, @lotdetail.all, :Lotnumber,:Lotnumber,prompt: "Select the Yarn")%>
<%end%>

我错过了什么吗? 提前致谢

2 个答案:

答案 0 :(得分:-1)

知道$(".lot").append("<%= escape_javascript render(partial: 'colordata') %>")通常应该有效,我检查了您的previous question

如果是同一个问题,请删除ajax调用中的dataType: JSON,一切正常。

答案 1 :(得分:-1)

感谢。我通过用

更新colordata.js.erb解决了这个问题
$(".lot").append("<%= j render('colordata') %>" );