我有以下 ActiveAdmin 模型
class AdminUser < ActiveRecord::Base
has_many :challenges
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end
和以下挑战模型
class Challenge < ActiveRecord::Base
belongs_to :subdomain
belongs_to :admin_user
has_many :comments, :dependent => :destroy
end
我在我的 application.html.erb 中定义了以下方法
helper_method :all_admin_users
def all_admin_users
@users = AdminUser.all
end
Adminuser 有以下属性:id,:name,:email 现在,当我尝试访问特定用户的挑战时
<ul>
<% all_admin_users.each do |user| %>
<li> <%= link_to user.name ,user.challenges%></li>
<br />
<% end %>
</ul>
我收到以下错误“Challenge::ActiveRecord_Associations_CollectionProxy 的未定义方法‘to_model’”
这是我的 routes.rb 文件
Rails.application.routes.draw do
resources :comments
devise_for :admin_users, ActiveAdmin::Devise.config
get 'subdomains/index'
get 'subdomains/edit'
get 'subdomains/new'
get 'subdomains/show'
get 'home/index'
root 'home#index'
resources :challenges
resources :subdomains
ActiveAdmin.routes(self)
end
最佳答案
首先,在您的routes
文件中,您需要添加操作以列出用户质疑
在routes.rb
末尾添加
get 'users/:user_id/challenges' => 'challenges#user_challenges', as: :user_challenges
# this way you will have action to view users challenges
# inside your challenges controller
在您的challenges_controller.rb
中添加操作user_challenges
def user_challenges
@challenges = Challenge.where(admin_user_id: params[:user_id])
end
为此操作创建 View ,并显示您的挑战
然后你可以为这个 Action 创建链接
<ul>
<% all_admin_users.each do |user| %>
<li> <%= link_to user.name , user_challenges_path(user.id)%> </li>
<br />
<% end %>
</ul>
关于ruby-on-rails - rails 中未定义的方法 `to_model',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045215/