user2911232
user2911232

Reputation:

Partial path error because of nil

I am working on a project and I just introduced polymorphism for posting text Posts and photo Posts.

When I try to post an image I get this error: "'nil' is not an ActiveModel-compatible object that returns a valid partial path."

enter image description here

The first show file (app/views/dashboards/show.html.erb) with code:

<%= form_for @text_post do |form| %>
<%= form.text_field :body, placeholder: 'Post content here' %>
<%= form.submit 'Post Title!' %>
<%end%>

<%= form_for @photo_post do |form| %>
<%= form.file_field :image %>
<%= form.submit 'Post Image!' %>
<%end%>

<%= render @posts %>

which leads to this at app/views/photo_posts/_photo_post.html.erb with the following code:

<%= image_tag photo_post.image.url(:post) %>

The app/views/posts/_post.html.erb which causes problem is:

<%= div_for post do %>
<%= link_to post.user.username, post.user %>
posted
<%= render post.content %>
<%= link_to time_ago_in_words(post.created_at), post %>
<% end %>

Sorry if the details I provide is insufficient, I am a new ROR developer. If you want, you can fork or check the project from here, (I pushed so you can check for the error).

Any help/guidance is greatly appreciated!

Upvotes: 0

Views: 125

Answers (1)

maximus ツ
maximus ツ

Reputation: 8065

Please try

<%= render post.content if post.content %>

instead of

<%= render post.content %>

as per the error logs, this should fix the issue.

Upvotes: 1

Related Questions