0

I am trying to append colordata partial in the main form but unable to do so. Here's my controller

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

here's my js file which is present in views named as colordata.js.erb

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

here's my main form where I have to append

<%= 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>

Here's the output of terminal

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)

here's my partial '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%>

Did I miss something? Thanks in advance

Amitoj Singh
  • 121
  • 1
  • 1
  • 7

2 Answers2

-1

Knowing that $(".lot").append("<%= escape_javascript render(partial: 'colordata') %>") should normally work, i checked your previous question.

If it's the same issue, then remove the dataType: JSON in your ajax call and everything should be ok.

Sovalina
  • 5,410
  • 4
  • 22
  • 39
  • Have you tried to see what your success request returns here: `success: function(data){ console.log(data) }` ? – Sovalina May 07 '18 at 13:48
-1

Thanks. I solved it by updating the colordata.js.erb with

$(".lot").append("<%= j render('colordata') %>" );
Amitoj Singh
  • 121
  • 1
  • 1
  • 7