Rails路由,在同一页面上显示所有元素

时间:2014-06-09 09:13:27

标签: ruby custom-routes ruby-on-rails-4.1 mongoid4

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

  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.

错误就在这里,在我的meal_controller.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

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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)