初学者在这里...我试图显示图像,但我得到的是未定义的局部变量或方法`user'"错误信息。我想我知道发生了什么 - 没有方法,但我似乎无法让它与我尝试过的方法一起正常工作。我有一个'项目'模特&控制器...和一个'版本'模特&控制器。 (很确定它只是一个控制器问题,所以我发布了这些文件......如果我需要发布其他文件,请告诉我)。我相信发生的事情是当用户创建新版本时没有设置user_id(至少当我运行" version.user_id"对于某个版本ID,它返回nil)。我该怎么办......
1)当登录用户创建版本时,确保在db中设置user_id? 2)确保版本索引中有一个方法,以便我可以显示该user_id的配置文件pic以创建版本??
提前感谢任何帮助...
错误:
NameError in Versions#index
Showing /Users/user/Documents/clones/collab/app/views/versions/_version.html.erb where line #3 raised:
undefined local variable or method `user' for #<#<Class:0x00000101b39f28>:0x0000010828c1d0>
版本控制器:
class VersionsController < ApplicationController
before_action :find_project
def new
@version = Version.new
end
def show
@project = Project.find(params[:project_id])
@version = Version.find(params[:id])
end
def index
# @user = User.where(:id => @version.user_id) first figure out how to set user_id on new versions (its nil now)
@versions = Version.paginate(page: params[:page])
end
def create
@project = Project.find(params[:project_id])
@version = @project.verions.build(version_params)
if @version.save
flash[:success] = "You've successfully added a version of this project"
redirect_to project_path(@project)
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def find_project
@project = Project.find(params[:project_id])
end
def version_params
params.require(:version).permit(:title, :project_id, :user_id)
end
end
版本新视图:
<% provide(:title, 'New Version') %>
<div class="row-fluid">
<div class="col-md-5 no-pad">
<h1>Add a new version to this project</h1>
<%= bootstrap_form_for @version, :url => project_versions_path do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :title %>
<div class="well">
<h4>Drag and Drop your file here:</h4>
<%= image_tag("wavicon-large-grey.png", alt: "add file") %>
</div>
<%= f.button "Create Now! ", class: "btn btn-lg btn-primary" %>
<% end %>
</div>
</div>
VERSIONS INDEX VIEW:
<% provide(:title, @versions ) %>
<div class="row-fluid">
<section class="no-pad col-md-9">
<%= render :partial => '/projects/project_header' %>
<h4>Version History</h4>
<%= will_paginate %>
<ul class="versions">
<% @project.versions.first(5).each do |version| %>
<%= render version %>
<% end %>
</ul>
<!-- CAN REFACTOR TO BELOW FOR CLEANER CODE
<ul class="versions">
<%= render @versions %>
</ul>
-->
<%= will_paginate %>
</section>
</div>
_VERSION.HTML.ERB PARTIAL(链接到上面的索引文件中)
<li>
<%= link_to project_version_path(@project, version) do %>
<h6><%= image_tag user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
<span class="timestamp pull-right">
Created <%= time_ago_in_words(version.created_at) %> ago
<span class="glyphicon glyphicon-time"></span>
</span>
<% end %>
</li>
VERSION.RB MODEL(不确定是否需要):
class Version < ActiveRecord::Base
belongs_to :project
belongs_to :user
validates :title, presence: true, length: { maximum: 140 }
default_scope -> { order('created_at DESC') }
end
答案 0 :(得分:3)
解决以下错误
undefined local variable or method 'user' for #<#<Class:0x00000101b39f28>:0x0000010828c1d0>
在_version.html.erb
部分
替换
<h6><%= image_tag user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
使用
<h6><%= image_tag version.user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
注意version.user.image_url
而不是user.image_url
。您收到错误,因为未定义user
变量。但是定义了version
,以便访问相关的user
记录,您只需使用version.user
即可。
<强>更新强>
User
模型中缺少与Version
模型的关联。设置如下:
class Version < ActiveRecord::Base
belongs_to :user
## ...
end
class User < ActiveRecord::Base
has_many :versions
## ...
end
更新2
存储user_id
实例的Version
。更新控制器中的new
操作,如下所示:
def new
@version = @project.versions.build(user_id: @project.user_id)
end
在版本new
视图中,为user_id
添加隐藏字段:
<%= f.hidden_field :user_id %>
更新版本index
视图,如下所示:
<ul class="versions">
<% @project.versions.first(5).each do |version| %>
<%= render partial: "version", object: version %>
<% end %>
</ul>
答案 1 :(得分:1)
你需要在_VERSION.HTML.ERB中执行:
<h6><%= image_tag @version.user.image_url(:collaborator_thumb).to_s, :class => "profile-pic-thumb" %><%= version.title %>...</h6>
由于您尚未在任何地方定义用户,因此您可以访问与user
相关联的@version
。
您还需要在has_many
和belongs_to
模型中加入Version
和User
关联。