Rails route, показать все элементы на одной странице
Мне нужно показать все мои элементы на одной странице. В маршрутах:
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.
Ошибка здесь, на моем foods_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 ответа
Решение
Маршрут ниже ожидает :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)
выше.
Я думаю, что вы должны передать параметр diet_id в параметрах. Вы должны попробовать что-то вроде этого: <%= link_to "Show all meals", nourishment_diet_show_all_meals_path(@diet.id), :class=>"button green" %>
, @diet.id
это всего лишь пример, используйте все, что работает для вашего приложения.