I can't retrieve the contents of the associated database.

Asked 2 years ago, Updated 2 years ago, 81 views

Ruby (Ruby on Rails) is creating a restaurant word-of-mouth site like a food log while referring to tutorials.I set categories for each area, but I failed to pull them from the controller on the search results.

NoMethodError in Restaurants
undefined method `restaurant'

·Research results (restaurants/search.rb)

<%@search_restaurants.each do |restaurant|%>
 <span><%=space.area.name%></span>
<%end%>

·Controllers (restaurants_controllers.rb)

def search
  @restaurants=Restaurant.paginate (page:params[:page])
  @category=Category.all
end

·Routing (routes.rb)

resources:categories, param::category_name do
  resources:restaurants, only: [:show]
end

I put in @category and found re/restaurants/1 」 in text.

I'm sorry, but I appreciate your cooperation.

ruby-on-rails ruby mvc

2022-09-29 21:40

1 Answers

I set the view as below and it was resolved.
<%=restaurant.category.try(:name)%>
It seems that the problem was that there were no categories in the database.


2022-09-29 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.