<i id='Fmdwz'><tr id='Fmdwz'><dt id='Fmdwz'><q id='Fmdwz'><span id='Fmdwz'><b id='Fmdwz'><form id='Fmdwz'><ins id='Fmdwz'></ins><ul id='Fmdwz'></ul><sub id='Fmdwz'></sub></form><legend id='Fmdwz'></legend><bdo id='Fmdwz'><pre id='Fmdwz'><center id='Fmdwz'></center></pre></bdo></b><th id='Fmdwz'></th></span></q></dt></tr></i><div id='Fmdwz'><tfoot id='Fmdwz'></tfoot><dl id='Fmdwz'><fieldset id='Fmdwz'></fieldset></dl></div>
  • <legend id='Fmdwz'><style id='Fmdwz'><dir id='Fmdwz'><q id='Fmdwz'></q></dir></style></legend>

    1. <tfoot id='Fmdwz'></tfoot>
      • <bdo id='Fmdwz'></bdo><ul id='Fmdwz'></ul>

      <small id='Fmdwz'></small><noframes id='Fmdwz'>

      1. 在 Rails 4 中显示所选类别的子类别

        Show the subcategories of the chosen category in Rails 4(在 Rails 4 中显示所选类别的子类别)
            <bdo id='W8PBP'></bdo><ul id='W8PBP'></ul>
          • <small id='W8PBP'></small><noframes id='W8PBP'>

            <i id='W8PBP'><tr id='W8PBP'><dt id='W8PBP'><q id='W8PBP'><span id='W8PBP'><b id='W8PBP'><form id='W8PBP'><ins id='W8PBP'></ins><ul id='W8PBP'></ul><sub id='W8PBP'></sub></form><legend id='W8PBP'></legend><bdo id='W8PBP'><pre id='W8PBP'><center id='W8PBP'></center></pre></bdo></b><th id='W8PBP'></th></span></q></dt></tr></i><div id='W8PBP'><tfoot id='W8PBP'></tfoot><dl id='W8PBP'><fieldset id='W8PBP'></fieldset></dl></div>
            <legend id='W8PBP'><style id='W8PBP'><dir id='W8PBP'><q id='W8PBP'></q></dir></style></legend>
          • <tfoot id='W8PBP'></tfoot>

                    <tbody id='W8PBP'></tbody>

                  本文介绍了在 Rails 4 中显示所选类别的子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  1.在我的 **view/gigs/new.html.erb 我使用

                   <%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %><%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, {prompt: "Choose a subcategory"} %>

                  它创建了这个

                  当点击下图时:

                  从上图可以看出,根据我选择的类别,只显示该类别拥有的子类别.

                  2.在我的演出控制器中,我编写了以下代码.

                  def update_sub_categories@cats = Subcategory.where(category_id: params[:category_id]).all响应(@cats)结尾

                  3.我必须在同一文件夹中创建一个文件view/gigs/update_sub_categories并把这段代码

                  $("#gig_subcategory_id").empty().append("<%= escape_javascript(render(:partial => "subcategory", :collection => @cats, :as => :cat)) %>")

                  也是同一文件夹中的部分 view/gigs/_subcategory.html.erb

                  <option value="<%= cat.id %>"><%= cat.name %></option>

                  4. 添加App/javascript/gigs.js.coffee

                   $(document).on 'change', '#gig_category_id', (evt) ->$.ajax 'update_sub_categories',类型:'获取'数据类型:'脚本'数据: {category_id: $("#gig_category_id option:selected").val()}错误:(jqXHR,textStatus,errorThrown)->console.log("AJAX 错误:#{textStatus}")成功:(数据,textStatus,jqXHR)->console.log("动态国家选择OK!")

                  5. 最后在路线

                  get 'gigs/update_sub_categories' =>'演出#update_sub_categories'

                  <块引用>

                  问题:一切正常,我选择类别,然后出现所选类别的子类别.但它只适用于views/gigs/new.html.erb,而 views/gigs/edit.html.erb 中没有,我做错了什么?

                  解决方案

                  查看您的控制台或 development.log,您将看到一些消息表明 rails 无法解析 update_sub_categories 当您脚本失败.当使用新表单调用该方法时,您会看到 rails 正在调用 your_controller/your_action/update_sub_categories ——您可能会看到它现在的情况.

                  您将不得不更新您的 routes.rb 以处理对该方法的裸"调用(当它缺少 your_action 和成员路由时.成员路由的显式处理是必要的,因为您的编辑使用嵌入在路由中的 ID 字段,而不是在末尾附加它.

                  routes.rb 中的当前路由替换为以下内容,您应该状态良好:

                   获取 'update_sub_categories' =>'你的模型#update_sub_categories'得到你的模型/更新子类别"=>'你的模型#update_sub_categories'资源:your_model 做获取 :update_sub_categories, :on =>:成员结尾

                  显然,在我的示例中,您将希望将您的模型名称替换为字符串 your_model.

                  1. In my **view/gigs/new.html.erb i use

                   <%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
                   <%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, {prompt: "Choose a subcategory"} %>
                  

                  It creates this

                  and when clicked the below image:

                  From the picture above as you see depending on what category i choose,just the subcategories owned by that category are displayed.

                  2. In my gig controller for this to work,i wrote the below code.

                  def update_sub_categories
                    @cats = Subcategory.where(category_id: params[:category_id]).all
                    respond_with(@cats)
                  end
                  

                  3. I had to create a file in the same folder view/gigs/update_sub_categories and put this code

                  $("#gig_subcategory_id").empty().append("<%= escape_javascript(render(:partial => "subcategory", :collection => @cats, :as => :cat)) %>")
                  

                  Also the partial in the same folder view/gigs/_subcategory.html.erb

                  <option value="<%= cat.id %>"><%= cat.name %></option>
                  

                  4. Add in App/javascript/gigs.js.coffee

                    $(document).on 'change', '#gig_category_id', (evt) ->
                      $.ajax 'update_sub_categories',
                        type: 'GET'
                        dataType: 'script'
                        data: {
                          category_id: $("#gig_category_id option:selected").val()
                        }
                        error: (jqXHR, textStatus, errorThrown) ->
                          console.log("AJAX Error: #{textStatus}")
                        success: (data, textStatus, jqXHR) ->
                          console.log("Dynamic country select OK!")
                  

                  5. Finally in routes

                  get 'gigs/update_sub_categories' => 'gigs#update_sub_categories'
                  

                  Question: Everything works, i choose the category and the subcategory of the chosen category appear.But it works just in views/gigs/new.html.erb,and doesn't in views/gigs/edit.html.erb, what am I doing wrong?

                  解决方案

                  Look in your console or development.log and you're going to see some message indicating that rails could not resolve update_sub_categories when your script fails. Wheras when calling that method with a new form, you'll see that rails is calling your_controller/your_action/update_sub_categories -- you can probably see where this is going now.

                  You're going to have to update your routes.rb both to handle a "naked" call to the method (when it's missing your_action and for member routing. Explicit handling of member routing is necessary because your edit uses an ID field embedded in the route, rather than appending it at the end.

                  Replace your current routing in routes.rb with the following and you should be in good shape:

                    get 'update_sub_categories' => 'your_model#update_sub_categories'
                  
                    get 'your_model/update_sub_categories' => 'your_model#update_sub_categories'
                  
                    resources :your_model do
                      get :update_sub_categories, :on => :member
                    end
                  

                  Obviously, you're going to want to substitute your model name for the string your_model in my example.

                  这篇关于在 Rails 4 中显示所选类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社

                          <tbody id='TCyEY'></tbody>

                        <small id='TCyEY'></small><noframes id='TCyEY'>

                          <legend id='TCyEY'><style id='TCyEY'><dir id='TCyEY'><q id='TCyEY'></q></dir></style></legend>
                          <tfoot id='TCyEY'></tfoot>

                        1. <i id='TCyEY'><tr id='TCyEY'><dt id='TCyEY'><q id='TCyEY'><span id='TCyEY'><b id='TCyEY'><form id='TCyEY'><ins id='TCyEY'></ins><ul id='TCyEY'></ul><sub id='TCyEY'></sub></form><legend id='TCyEY'></legend><bdo id='TCyEY'><pre id='TCyEY'><center id='TCyEY'></center></pre></bdo></b><th id='TCyEY'></th></span></q></dt></tr></i><div id='TCyEY'><tfoot id='TCyEY'></tfoot><dl id='TCyEY'><fieldset id='TCyEY'></fieldset></dl></div>
                            <bdo id='TCyEY'></bdo><ul id='TCyEY'></ul>