To resolve following error
undefined local variable or method 'user' for #<#<Class:0x00000101b39f28>:0x0000010828c1d0>
In your _version.html.erb
partial,
Replace
<h6><%= image_tag user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
With
<h6><%= image_tag version.user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
Note version.user.image_url
instead of user.image_url
. You are getting error because user
variable is not defined. BUT version
is defined so to access the associated user
record you can simply use version.user
.
UPDATE
The association with User
model is missing in Version
model. Set it as below:
class Version < ActiveRecord::Base
belongs_to :user
## ...
end
class User < ActiveRecord::Base
has_many :versions
## ...
end
UPDATE 2
To store the user_id
for a Version
instance. Update the new
action in your controller as below:
def new
@version = @project.versions.build(user_id: @project.user_id)
end
In the versions new
view, add a hidden field for user_id
:
<%= f.hidden_field :user_id %>
Update the versions index
view as below:
<ul class="versions">
<% @project.versions.first(5).each do |version| %>
<%= render partial: "version", object: version %>
<% end %>
</ul>