为什么我会收到以下错误?
nil
不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path。
我认为该错误可能与我正在使用 Rails 3.2 而我使用 Rails 4 的教程有关。
这是模型代码:
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) %>