在Rails 4中显示所选类别的子类别 [英] Show the subcategories of the chosen category in Rails 4

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

问题描述

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

 <%= f.collection_select :category_id,Category.all,:id,:name,{prompt:选择类别}%> 
<%= f.collection_select:subcategory_id,Subcategory.all,:id,:name,{prompt:选择子类别}%>

它创建了





,点击下面的图片:





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



strong> 2。在我的gig控制器中,我写了以下代码。

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

3。我必须在同一个资料夹中建立档案 view / gigs / update_sub_categories
此代码

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

/gigs/_subcategory.html.erb

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

strong> 4。在应用/ 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()
}
错误:(jqXHR,textStatus,errorThrown) - >
console.log(AJAX Error:#{textStatus})
success:(data,textStatus,jqXHR) - >
console.log(Dynamic country select OK! $ b





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




问题:一切正常,我选择类别和所选类别的子类别出现。但它只在
views / gigs / new.html.erb 中工作,而不在 view / gigs / edit.html.erb ,我做错了什么?



解决方案

查看你的控制台或development.log,你会看到一些消息,指出rails不能解决 update_sub_categories 当你的脚本失败。当使用新表单调用该方法时,你会看到rails正在调用 your_controller / your_action / update_sub_categories - 你可能会看到这是现在正在进行。 p>

您必须更新 routes.rb 才能处理对方法的裸露调用当它缺少 your_action 和成员路由时,成员路由的显式处理是必要的,因为您的编辑使用 ID



routes.rb 中的当前路由替换为并且您应该处于良好状态:

  get'update_sub_categories'=>'your_model#update_sub_categories'

get'your_model / update_sub_categories'=>'your_model#update_sub_categories'

资源:your_model do
get:update_sub_categories,:on =>:member
end $显然,你要用你的模型名替换字符串。b $ b



< 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中显示所选类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆