在Rails中出现了未定义的方法`to_model`。

4
我有以下的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>

我遇到了以下错误:“undefined method `to_model' for Challenge::ActiveRecord_Associations_CollectionProxy”。这是我的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

错误 "undefined method `to_model' for #Challenge::ActiveRecord_Associations_CollectionProxy:0x007fc54980a2a8" - pokiri
最好您编辑您的问题并将截断的句子放回问题中,而不是放在评论中。 - Max Williams
3个回答

3

首先在您的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

创建视图以显示您的挑战,并为此操作创建链接。
<ul>
  <% all_admin_users.each do |user| %>
    <li> <%= link_to user.name , user_challenges_path(user.id)%> </li>
    <br />
  <% end %>
</ul>

谢谢,但这会给我所有用户的挑战。 我希望显示用户名,并在单击用户名时 我希望它直接指向属于所点击用户的挑战。 - pokiri
你想重定向到该用户的所有挑战列表吗?请展示给我routes文件夹。 - Nermin
谢谢 我尝试过了 但是challenges_path会给我所有用户的挑战 我想要的是特定用户的挑战? - pokiri
它给了我一个缺少模板的错误,URL 重定向到“http://localhost:3000/users/1/challenges”。 - pokiri
你需要为添加的操作创建视图,就像我所说的那样。 - Nermin
我刚刚在user_challenges操作中包含了render:index,它起作用了,非常感谢@Nermin。 - pokiri

1

使用user.challenge而不是user.challenges

更新

<ul>
  <% all_admin_users.each do |user| %>
    <% user.challenges.each do |challenge| %>
      <li> <%= link_to user.name ,challenges_path(user_id: user.try(:id)) %></li>
      <br />
    <% end %>
  <% end %>
</ul>

challenges_controller.rb

def index
  @challenges = Challenge.where(admin_user_id: params[:user_id])
end

谢谢,但我遇到了以下错误:"undefined method `challenge' for #<AdminUser>"。 - pokiri
Challenge表中,你有admin_user_id吗? - Prashant4224
是的@prashant 这是Challenge表中列的列表 ["id", "title", "body", "input_format", "output_format", "constraints", "subdomain_id", "admin_user_id", "created_at", "updated_at"] - pokiri
我尝试了user.try(:challenge),但是它没有将我重定向到任何地方!!! - pokiri
我已经更新了帖子,请仔细阅读。希望能对你有所帮助!@pokiri - Prashant4224
谢谢@prashanth 假设我有两个用户,每个用户都有两个挑战 根据您的代码 它给我所有4个挑战的链接,标题名称为USER NAMES 但是我需要的是 您需要显示用户的名称(在这种情况下仅有2个用户),当我们单击用户名时,它应将我重定向到该特定用户的所有挑战 - pokiri

0

作为对Prashant4020的回答的补充 - 你认为更新用户模型怎么样:

class User < ActiveRecord::Base
...
belongs_to :admin_user
...

因此

class AdminUser < ActiveRecord::Base
has_many :challenges
has_many :users
.... 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接