Search code examples
rubycustom-routesruby-on-rails-4.1mongoid4

Rails route, show all elements on the same page


I need to show all my elements on the same page. In routes:

  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

which will generate:

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

The problem, when I do this:

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

This error raise:

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.

The error is here, on my meals_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

Method:

  def show_all_meals
    puts "This word does not appear"
  end

Can someone help me?


Solution

  • The route below expects a :diet_id. A diet instance has to be provided as an argument for this path to call corresponding action.

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

    This should be changed:

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

    to:

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

    Notice the argument (diet) above.