jQuery在本地工作正常,但不是heroku

时间:2019-05-30 00:21:32

标签: jquery ruby-on-rails ajax

在我的Rails应用程序表单中,我尝试向我的控制器发送一个jquery请求,以在选择了某个字段时更新下拉列表,它在本地可以正常运行,但在heroku上不能正常运行。

我尝试过RAILS_ENV=production rake assets:precompile,但没有任何反应。

它曾经在我添加另一个名为area的字段之前起作用。

我的表格

<head>
  <script src="/assets/mama.js"></script>
</head>
<%= form_with(model: shop, local: true) do |form| %>
<% if shop.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(shop.errors.count, "error") %> prohibited this shop from being saved:</h2>
  <ul>
    <% shop.errors.full_messages.each do |message| %>
    <li><%= message %></li>
    <% end %>
  </ul>
</div>
<% end %>
<body>
  <form >
    <h1>Add Your Shop</h1>
    <fieldset>
      <legend><span class="number">1</span>Your basic info</legend>
      <label for="name">Name:</label>
      <%= form.text_field :name, id: :shop_name %>
      <label for="mail">Email:</label>
      <%= form.text_field :email, id: :shop_email %>
      <label for="password">Description:</label>
      <%= form.text_area :description, id: :shop_description %>
      <label>Image:</label>
      <%= form.file_field :imageshop, id: :shop_imageshop %>
    </fieldset>
    <fieldset>
      <legend><span class="number">2</span>Important Infos</legend>
      <label for="bio">Location:</label>
      <%= form.text_field :location, id: :shop_location %>
    </fieldset>
    <fieldset>
      <label for="bio">Registeration Number:</label>
      <%= form.text_field :registeration_number, id: :registeration_number %>
    </fieldset>
    <fieldset>
      <label for="job">Website:</label>
      <%= form.text_field :web, id: :shop_web %>
      <label>Phone Number:</label>
      <%= form.text_field :phone, id: :shop_phone %><br><label class="light" for="business">Business Type</label>
      <%= form.select :busness, ['Service Provider', 'Products Based Business'] %><label class="light" for="design">Business Category</label><br>
      <%= form.select :category, ['Electronics', 'Fashion', 'Furniture', 'Kitchen Stores', 'Restraunts', 'Hotels', 'General Stores', 'Grocery Stores'] %>
      <label>Please select a State:</label>
      <%= form.select :state, options_for_select([["Select a State",""]] + State.all.map { |c| [c.name, c.id] },selected:  shop.state ), {}, id: "state"%>
      <label>Please select a District:</label>
      <%= form.select :city, options_for_select([["Select a District",""]]),{}, :id => 'city' %>
      <label>Please select a Area:</label>
      <%= form.select :area, options_for_select([["Select a Area",""]]),{}, :id => 'area' %>
    </fieldset>
    <div class="bobo" style="text-align:center; padding:0; margin:0;">
      <%= form.submit class: "btn-success" %>
    </div>
  </form>
</body>
</html>
<% end %>

我的mama.js

<script type="text/javascript">
var  selectedCity = "";
<% if shop.city.present? %>
  selectedCity = <%= shop.city  %>;
<% end %>
var  selectedArea = "";
<% if shop.area.present? %>
selectedArea = <%= shop.area  %>;
<% end %>
$(function() {
  if ($("select#state").val() == "") {
    $("select#city option").remove();
    var row = "<option value=\"" + "" + "\">" + "city" + "</option>";
    $(row).appendTo("select#city");
   }
   var $val = $("select#state").val();
   if($val != ""){
     getCitiesOfState($val);
   }

   $("select#state").change(function() {
    var id_value_string = $(this).val();
    if (id_value_string == "") {
     $("select#city option").remove();
     var row = "<option value=\"" + "" + "\">" + "city" + "</option>";
     $(row).appendTo("select#city");
    } else {
     // Send the request and update city dropdown
      getCitiesOfState(id_value_string)
    }
   });


  if ($("select#city").val() == "" && selectedCity == "") {
    $("select#area option").remove();
    var row = "<option value=\"" + "" + "\">" + "area" + "</option>";
    $(row).appendTo("select#area");
   }
   var $val2 = $("select#city").val();
   console.log("val________", $val2);
   if($val2 != "" && selectedCity == ""){
    getAreasOfCity($val2)
   }
   console.log("selected_city________", selectedCity);
   if (selectedCity != "") {

    getAreasOfCity(selectedCity)
   }

    $("select#city").change(function() {
      var id_value_string = $(this).val();
      if (id_value_string == "") {
        $("select#area option").remove();
        var row = "<option value=\"" + "" + "\">" + "area" + "</option>";
        $(row).appendTo("select#area");
      } else {
        // Send the request and update city dropdown
        getAreasOfCity(id_value_string)

      }
    });


  });  
</script>

我的控制器

#get cities from state
  def get_cities_by_state
    @cities = State.find(params[:state]).cities
    respond_to do |format|
      format.json { render :json => @cities }
    end
  end 

  #get area from city
  def get_areas_by_city
    @areas = City.find(params[:city]).areas
    respond_to do |format|
      format.json { render :json => @areas }
    end
  end

我的路线

post :get_cities_by_state,       action: :get_cities_by_state, controller: :products
  post :get_areas_by_city,       action: :get_areas_by_city, controller: :products

0 个答案:

没有答案