0

我需要在同一页面上显示我的所有元素。在路线:

  namespace :nourishment do
   resources :diets do
     resources :nourishment_meals, :controller => 'meals'
     get 'nourishment_meals/show_all_meals' => 'meals#show_all_meals', as: "show_all_meals"
   end
  end

这将产生:

nourishment_diet_nourishment_meals_path  GET     /nourishment/diets/:diet_id/nourishment_meals(.:format)     nourishment/meals#index
POST     /nourishment/diets/:diet_id/nourishment_meals(.:format)     nourishment/meals#create
new_nourishment_diet_nourishment_meal_path   GET     /nourishment/diets/:diet_id/nourishment_meals/new(.:format)     nourishment/meals#new
edit_nourishment_diet_nourishment_meal_path  GET     /nourishment/diets/:diet_id/nourishment_meals/:id/edit(.:format)    nourishment/meals#edit
nourishment_diet_nourishment_meal_path   GET     /nourishment/diets/:diet_id/nourishment_meals/:id(.:format)     nourishment/meals#show
PATCH    /nourishment/diets/:diet_id/nourishment_meals/:id(.:format)     nourishment/meals#update
PUT  /nourishment/diets/:diet_id/nourishment_meals/:id(.:format)     nourishment/meals#update
DELETE   /nourishment/diets/:diet_id/nourishment_meals/:id(.:format)     nourishment/meals#destroy


[**THIS**]
    nourishment_diet_show_all_meals_path     GET     /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format)  nourishment/meals#show_all_meals

问题,当我这样做时:

<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>

此错误引发:

Problem:


Problem:
  Document(s) not found for class NourishmentMeal with id(s) show_all_meals.
Summary:
  When calling NourishmentMeal.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): show_all_meals ... (1 total) and the following ids were not found: show_all_meals.
Resolution:
  Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.

错误在这里,在我的餐点控制器.rb

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_nourishment_meal
      @nourishment_diet = NourishmentDiet.find(params[:diet_id])
     [***HERE***] @nourishment_meal = @nourishment_diet.meals.find(params[:id])
    end

方法:

  def show_all_meals
    puts "This word does not appear"
  end

有人能帮我吗?

4

2 回答 2

0

我认为你应该在 params 中传递diet_id 参数。你应该尝试这样的事情:<%= link_to "Show all meals", nourishment_diet_show_all_meals_path(@diet.id), :class=>"button green" %>. @diet.id只是一个示例,请使用适用于您的应用程序的任何内容。

于 2014-06-09T09:20:54.143 回答
0

下面的路线需要一个:diet_id. 必须提供一个diet实例作为该路径的参数才能调用相应的操作。

nourishment_diet_show_all_meals_path     GET     /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format)  nourishment/meals#show_all_meals

这应该改变:

<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>

至:

<%= link_to "Show all meals", nourishment_diet_show_all_meals_path(diet), :class=>"button green" %>

注意上面的论点(diet)

于 2014-06-09T09:21:51.117 回答