初學者在這裏...我試圖顯示一個圖像,但我得到「未定義的局部變量或方法`用戶'」的錯誤信息。我想我知道發生了什麼 - 沒有任何方法,但我似乎無法使它與我所嘗試過的正確工作。我有一個'項目'模型&控制器...和'版本'模型&控制器。 (很確定它只是一個控制器問題,所以我發佈了這些文件...只是讓我知道如果我需要發佈其他文件)。我相信發生了什麼是用戶創建一個新版本user_id沒有被設置(至少當我運行「version.user_id」爲某個版本ID,它返回零)。我將如何去...未定義的本地變量或方法`用戶'軌控制器問題
1)確保在登錄的用戶創建一個版本時,在數據庫中設置user_id? 2)確保版本索引中有一個方法,以便我可以顯示該user_id的配置文件圖像以創建版本?
在此先感謝您的幫助......
出現錯誤:
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索引視圖:
<% 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部分的(鏈接到上述索引文件)
<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
Kirti,當我這樣做時,我得到一個類似的錯誤... NameError in Versions#index Showing /Users/user/Documents/clones/collab/app/views/versions/_version.html.erb where line#3對於#<#:0x000001057e3690> –
BB500
@ BB500:您還需要在版本和用戶模型中放置has_many和belongs_to關聯。 – Saurabh
我剛剛注意到,你說你有'version.user_id'字段,但是你沒有在模型中設置關聯。 –