I am new in RoR and i am using helper_method
to pass data from ActiveRecord. My code is follow:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :main_menus
def main_menus
@main_menus = Category.all
end
end
View: application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title) %></title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<div id="wrapper">
<div class="wrapper-inner">
<%= render 'layouts/header' %>
<%= render 'layouts/menu' %>
<%= yield(:body) %>
</div>
</div>
<%= render 'layouts/javascript' %>
</body>
</html>
Menu.html.erb:
<%= main_menus.all do |menu| %>
<%= menu.title %>
<% end %>
Showing only this:
#<Category::ActiveRecord_Relation:0x00007f1c0e26c3d0>
Pls help me. Where is my error?
So, you're using all
to apparently iterate over the returned value in the main_menus
helper, which I guess should be each
, otherwise all
would be the equivalente to chain .all
to Category.all
.
Try instead:
<% main_menus.each do |menu| %>
<%= menu.title %>
<% end %>
Note you can simplify the helper method to just return Category.all
if you're not going to use the @main_menus
variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.