8

次のエラーが表示されるのはなぜですか?

nilActiveModel 互換オブジェクトではありません。:to_partial_path を実装する必要があります。

このエラーは、Rails 4 を使用しているときに Rails 3.2 を使用しているチュートリアルに関連している可能性があると思います。

モデルコードは次のとおりです。

class DashboardsController < ApplicationController
  def show
    @text_shout = TextShout.new
    @photo_shout = PhotoShout.new
    @shouts = current_user.shouts
  end
end

class PhotoShoutsController < ApplicationController
  def create
    content = build_content
    shout = current_user.shouts.build(content: content)
    if shout.save
      redirect_to dashboard_path
    else
      flash.alert = "Could not shout."
      redirect_to dashboard_path
    end
  end

  private
  def build_content
    PhotoShout.new(photo_shout_parameters)
  end

  def photo_shout_parameters
    params.require(:photo_shout).permit(:image)
  end
end

_shout.html パーシャルでエラーが発生したビュー コードを次に示します。

# app/view/dashboards/show.html.erb
<%= form_for @text_shout do |form| %>
   <%= form.text_field :body, placeholder: 'Shout content here' %>
   <%= form.submit 'Shout' %>
<% end %>

<%= form_for @photo_shout do |form| %>
   <%= form.file_field :image %>
   <%= form.submit 'Shout' %>
<% end %>

<%= render @shouts %>

# app/view/shouts/_shout.html.erb
<%= div_for shout do %>
  <%= link_to shout.user.username, shout.user %>
  shouted
                                 +---------------------------------+
  <%= render shout.content %> <--| ERROR "nil' is not an Active "  |
                                 | "Model-compatible object"       |
                                 +---------------------------------+
  <%= link_to time_ago_in_words(shout.created_at), shout %>
<% end %>

# app/views/photo_shouts/_photo_shout.html.erb
<%= image_tag photo_shout.image.url(:shout) %>
4

6 に答える 6

4

あなたが抱えている問題は、データベースにコンテンツが関連付けられていない既存のレコードがあるためです。これは、非ポリモーフィック セットアップからポリモーフィック セットアップに移行したために発生します。必要なことは、content_type と content_id が欠落しているシャウトを探して、データベースから削除することです。それらが削除されたら、追加すると便利です

validates_associated :コンテンツ

Shout モデルに追加して、将来のデータがデータベースを「破損」しないようにします。

于 2013-11-05T18:54:23.867 に答える
1

@shouts = current_user.shoutsこの行であなた@shoutsは次のように設定していますnil

を確認しcurrent_user.shoutsてください。nil として返されている必要があります

編集
代わりにこれを試してください

<%= render @shouts.content %>

于 2013-11-05T09:01:17.277 に答える
1

開発ログでこのエラーを見つけました。これはずっと問題でした。少しの間、私は非常に混乱していました。

[paperclip] An error was received while processing <Paperclip::Errors::CommandNotFoundError: Could not run the `identify` command. Please install ImageMagick.>

修正は実行するだけbrew update(オプション)のように見えbrew install imagemagickます。思考ボット チュートリアルの修正を探している他の人のために。

于 2014-08-08T22:21:07.467 に答える