未定义的局部变量或方法`user' rails控制器问题

时间:2014-04-29 20:18:03

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

初学者在这里...我试图显示图像,但我得到的是未定义的局部变量或方法`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

2 个答案:

答案 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_manybelongs_to模型中加入VersionUser关联。