简体   繁体   中英

ActiveRecord::Associations::CollectionProxy

I have a question. views/admins/show.html.erb

<% provide(:title , "施設") %>


  <p><%= @admin.name  %></p>
  <p><%= @admin.place %></p>
  <p><%= @admin.address %></p>
  <p><%= @admin.content  %></p>
<%= @dogs.each do |dog| %>
  <p><%= dog.name %></p>
  <p><%= dog.age  %></p>
  <p><%= dog.personality  %></p>
  <p><%= dog.breed  %></p>
  <p><%= dog.content  %></p>
<% end %>

and admins_controller

class AdminsController < ApplicationController
 before_action :authenticate_admin! , only:[:show]    
 def show
    #@admin = current_admin
    @admin=Admin.find(params[:id])
    @dogs = @admin.dogs
 end
end

When I watch the show.html.erb, The page's last sentence contains ActiveRecord::Associations::CollectionProxy. Why do it contain?

It's because you have the equals here, which tells rails to output it

<%= @dogs.each do |dog| %>

Make it

<% @dogs.each do |dog| %>

They hold the relation between Admin and Dog . It also provides many methods which can be used on the related Dog objects. Refer this for more details: http://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM