LoginSignup
7
4

More than 5 years have passed since last update.

ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. 解決方法

Posted at

いいね!(Like!)機能を作っていましたが、ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

私の場合、
show.html.erb
<%= link_to @blog.likes.count, liking_users_blog_path(@blog.id), id: "like-count" %>人がいいね!といっています

の場所から、
ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.)が出てしまいました。
そこでこのエラーメッセージの"nil"に注目致しました。
/liking_users.rb
<%= render @users %>

このviewファイルの中にある<%= render @users %>がnilというエラー画面によるヒントを元に、
解決しました。

このエラー解決手順の考え方は、
まず@usersの中がnilであるがために、<%= render @users %>が呼ばれても、
エラーとして機能しないのである。
おそらくここまでの過程をcodingしているのなら、
ある程度機能としては、うまくいっているだろうと思う。

私の場合、controllerに着目しました。
このviewファイルの<%= render @users %>を呼び出せるように、
定義しているかどうかを。

blogs_controller.rb
class BlogsController < ApplicationController
def liking_user
    @users = @blog.liking_users
end

def liking_userのuserをusersにすればいいことでした。
つまり、

blogs_controller.rb
class BlogsController < ApplicationController
def liking_users
    @users = @blog.liking_users
end

ということ。

ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.のエラーが出たら、renderしたい@xxxxxの定義をちゃんとコントローラーに定義しているか、もしくはどのように定義しているか、
確認しましょう!

参考にしたサイト

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4