Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails中的动态选择菜单_Ruby On Rails_Ajax - Fatal编程技术网

Ruby on rails RubyonRails中的动态选择菜单

Ruby on rails RubyonRails中的动态选择菜单,ruby-on-rails,ajax,Ruby On Rails,Ajax,提前感谢任何尝试或回答我问题的人。 我有城市模型,区域模型,用户模型。 城市模型和区域模型有很多联系 其中,作为区域模型和用户模型有很多通过关联 我希望在创建新用户时,根据所选城市显示区域选择菜单 当城市选择菜单中的任何项目被选中并发出请求时,区域选择菜单应显示属于该城市的区域 这是我的密码,请告诉我哪里错了。谢谢 (app/views/users/_form) <%= form_for(@user, remote: true) do |f| %> <div class="

提前感谢任何尝试或回答我问题的人。 我有城市模型,区域模型,用户模型。 城市模型和区域模型有很多联系 其中,作为区域模型和用户模型有很多通过关联

我希望在创建新用户时,根据所选城市显示区域选择菜单 当城市选择菜单中的任何项目被选中并发出请求时,区域选择菜单应显示属于该城市的区域

这是我的密码,请告诉我哪里错了。谢谢

(app/views/users/_form)
<%= form_for(@user, remote: true) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
<p>
<%=f.collection_select :city_id, City.order(:name), :id, :name, include_blank: true%>
</p>

<div class="a"></div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
config/routes.rb

app/views/users/_customTemplate.html.erb

最后但并非最不重要。。。 app/views/users/custom.js.erb

帮助 ruby服务器控制台错误

2015年9月30日开始为127.0.0.1版本提供GET/users/custom?city_id=1 21:20:01+0530由UsersController处理显示为HTML 参数:{city_id=>1,id=>custom}用户加载0.3ms 从users.id=?限制1[[id, 0]]在76ms ActiveRecord中找不到已完成的404:0.3ms

ActiveRecord::RecordNotFound找不到“id”=自定义的用户: app/controllers/users\u controller.rb:77:in'set\u user'

渲染 /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action\u dispatch/middleware/templates/rescues//u source.erb 4.1ms

浏览器控制台错误

获取404未找到发送@ jquery-4075e3b7b3964707c653f61b66f46dec.js?body=1:9665jQuery.extend.ajax @jquery-4075E3B7B39647C653F61B66F46Dec.js?body=1:9216匿名 作用@ users-c743c610a872f962fb75a7ac244905cd.js?body=1:4jQuery.event.dispatch @ jquery-4075E3B7B39647C653F61B66F46Dec.js?body=1:4671elemData.handle @ jquery-4075E3B7B39647C653F61B66F46Dec.js?body=1:4339列表选择器。\u handleMouseUp @关于:空白:544用户-c743c610a872f962fb75a7ac244905cd.js?正文=1:11 AJAX错误:错误


好吧,有太多的问题要我去挑;这就是我要做的:

你在找一种叫做。你在标题中提到了这一点,我只是想澄清一下术语

当您更改选择框中的值时,动态选择菜单应发送ajax请求。该过程应简洁明了,即您只向控制器发送一个ID,并最终返回一组数据,以填充下一个选择框:

#config/routes.rb
resources :cities do
    get "update", to: "users#custom" #-> url.com/cities/:city_id/update
end
resources :users #-> put this below so that the users/show method doesn't catch the above calls

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def custom
      @city = City.find params[:city_id]
      respond_to do |format|
        format.js
      end
   end
end

#app/views/users/custom.coffee
$('.a').append("<%=j render 'users/customTemplate' %>");
上面是后端。这是前端:

#app/views/users/_form.html.erb (removed HTML to clean it up for this)
<%= form_for(@user, remote: true) do |f| %>
    <%= f.text_field :name %>
    <%= f.collection_select :city_id, City.order(:name), :id, :name, include_blank: true %>
    <%= f.submit %>
<% end %>

#app/assets/javascripts/users.coffee
$(document).on 'change', '#user_city_id', (evt) ->
    $.ajax '/cities/' + $(this).val() + '/update',
      type: 'GET'
      dataType: 'html'
      data: {
        city_id: $('#user_city_id :selected').val()
      }
      error: (jqXHR, textStatus, errorThrown) ->
        console.log("AJAX Error: #{textStatus}")
      success: (data, textStatus, jqXHR) ->
        console.log("Dynamic country select OK!")
您将从中受益匪浅:


这个例子对我很有用。。然而,我不得不将我的collection_select转换为just select,但它的工作效率永远不会降低,感谢所有试图解决这个问题的人


不确定它是否解决了您的问题,但是,@tilu=params[:city\u id]应该是@tilu=params[:user][:city\u id]。谢谢Pavan,但它也不起作用。正在中断的代码不包括在内。您的错误发生在用户控制器中的操作集用户中。您正在尝试查找id为custom的用户-无法工作。此id可能来自url的自定义部分。Rails认为您希望在这里处理用户的实例。您的路线有问题。可能我的路线文件已经存在。。让我编辑我的控制器文件是的,谢谢,我已经通过了这个编播,但是他们使用分组的集合选择,所以所有的数据一次加载..我已经尝试了你的修改。。。rails服务器控制台错误为==启动GET/cities/1/update?city_id=1 for 127.0.0.1于2015-09-30 22:21:55+0530由UsersControllercustom处理为HTML参数:{city_id=>1}city Load 0.6ms选择城市。*来自cities.id=?LIMIT 1[[id,1]]在16ms ActiveRecord中完成了500个内部服务器错误:0.6ms NoMethodError undefined method each'for:app/controllers/users\u controller.rb:6:in custom'your calling.each on单个对象。摆脱.each循环,它应该在2015-10-01 09:45:17+0530由UsersControllercustom处理为HTML参数时为127.0.0.1启动Get/cities/1/update?city_id=1:{city_id=>1}城市加载0.2ms选择城市。来自cities.id=?限制1[[id,1]]渲染用户/_customTemplate.html.erb 0.2ms渲染用户/_customTemplate.html.erb 0.1ms渲染用户/custom.js.erb 4.5ms在10ms视图中完成200 OK:7.3ms | ActiveRecord:0.2ms*几乎没有,但有2个新问题1如何将@user f传递给模板,2它在新用户页面上没有显示我试图只显示值
Rails.application.routes.draw do
  resources :users
  resources :areas
  resources :cities
    get 'users/custom'
<%=f.collection_select :area_id, Area.where(city_id: @tilu), :id, :name, include_blank: true %>
$('.a').append("<%=j render 'customTemplate' %>");
#config/routes.rb
resources :cities do
    get "update", to: "users#custom" #-> url.com/cities/:city_id/update
end
resources :users #-> put this below so that the users/show method doesn't catch the above calls

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def custom
      @city = City.find params[:city_id]
      respond_to do |format|
        format.js
      end
   end
end

#app/views/users/custom.coffee
$('.a').append("<%=j render 'users/customTemplate' %>");
#app/views/users/_form.html.erb (removed HTML to clean it up for this)
<%= form_for(@user, remote: true) do |f| %>
    <%= f.text_field :name %>
    <%= f.collection_select :city_id, City.order(:name), :id, :name, include_blank: true %>
    <%= f.submit %>
<% end %>

#app/assets/javascripts/users.coffee
$(document).on 'change', '#user_city_id', (evt) ->
    $.ajax '/cities/' + $(this).val() + '/update',
      type: 'GET'
      dataType: 'html'
      data: {
        city_id: $('#user_city_id :selected').val()
      }
      error: (jqXHR, textStatus, errorThrown) ->
        console.log("AJAX Error: #{textStatus}")
      success: (data, textStatus, jqXHR) ->
        console.log("Dynamic country select OK!")