link_to通过关联模型

时间:2012-03-19 20:52:47

标签: ruby-on-rails ruby-on-rails-3 associations link-to

  • ruby​​ 1.9.2p290
  • rails 3.1.1

我有两个模型:RECIPE和RECIPE_CATEGORY分别与“belongs_to”和“has_many”关联。

我可以通过这个网址很好地按类别列出所有食谱:

http://localhost:3001/recipes/salads

但是在我的“link_to”中,按类别指向食谱列表似乎只在您自己的行动中起作用:食谱#list_by_category

<%= link_to recipe.recipe_category.name, recipe_category_recipes_path(@recipe_categories) %>

的RecipesController

def index  
 @recipes = Recipe.where({ :status_id => 1 }).includes(:chef, :recipe_category).order("updated_at desc").page(params[:page]).per(
end 

def list_by_category
 @recipe_category = RecipeCategory.find_by_name_plural(params[:recipe_category_id])
 @recipes = @recipe_category.recipes.where(:status_id => 1).includes(:chef).order("id desc").page(params[:page]).per(9)
end

路线

resources :recipes, :id => /[0-9]+/ do
 match 'pagina/:page', :action => :index, :on => :collection # Kaminari
 # list of recipes by category
 get 'recipe_category', :to => 'recipes#list_by_category', :path => ':recipe_category_id', :on => :collection, :recipe_category_id => /[a-z]+/
end

所需网址

  • localhost:3001 / recipes / - 食谱列表
  • localhost:3001 / recipes / 21681 - 显示食谱的页面
  • localhost:3001 / recipes / salads - 按类别显示食谱
  • localhost:3001 /食谱/午餐 - 按小时显示食谱

那么,如何构建一个“link_to”,指向所有操作中按类别划分的食谱?

我清楚了吗?如果我没有,请告诉我。

修改

的RecipesController

def index
  if params[:category_id]
    @category = Category.find_by_slug(params[:category_id])
    @recipes = @category.recipes.where(:status_id => 1).includes(:chef).order("updated_at desc").page(params[:page]).per(9)
  else
    @recipes = Recipe.where({ :status_id => 1 }).includes(:chef, :category).order("updated_at desc").page(params[:page]).per(9)
    @count_all = Recipe.where({ :status_id => 1 }).count()
  end
end

路线

resources :categories, :path => "recipes/categories", :only => :index do
  resources :recipes, :path => "", :only => :index do
    match 'pagina/:page', :action => :index, :on => :collection
  end
end
resources :recipes, :path => 'receitas', :id => /[0-9]+/ do
   match 'pagina/:page', :action => :index, :on => :collection
end

URLS

  • localhost:3001 / recipes - 列出所有食谱
  • localhost:3001 / recipes / categories - 列出所有类别
  • localhost:3001 / recipes / categories / salads * - 按类别列出食谱

<%= link_to recipe.name, category_recipes_path(recipe.category.slug) %>

1 个答案:

答案 0 :(得分:1)

如果您想要链接到列出给定类别中所有食谱的页面,那么最有效的方法是将食谱设置为recipe_category的嵌套资源:http://guides.rubyonrails.org/routing.html#nested-resources

路由看起来像:

resources :recipe_categories do
  resources :recipes
end

这将允许您使用如下链接:

recipe_category_recipes_path(recipe_category)

将路由到将显示recipe_category中所有配方的页面。如果这不是您想要的,请在您的问题中澄清。

修改

您可以使用索引url的参数,并在索引中添加一个catch,它会将标准索引视图向下过滤为仅匹配参数的配方。

所以你的rails链接调用看起来像这样:

recipes_path(:category => @category)
recipes_path(:time => @time)

这会导致网址看起来像这样:

localhost:3001/recipes?category=salads
localhost:3001/recipes?time=lunch

您的食谱控制器中的索引方法可以更改为:

def index 
    if params[:category]
        Recipe.where("category = ?", params[:category])
    else if params[:time]
        Recipe.where("time = ?", params[:time])
    else 
        @recipes = Recipe.all
    end
end 

这不是最漂亮的,我不确定这种方法有多“轨道”,但在这些情况下它对我有用。如果这是您正在寻找的,或者如果我再次错过,请告诉我。