创建新记录时存储错误的user_id

时间:2014-04-29 23:09:11

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

所以刚刚得到了一些帮助,但现在有一个新问题......我正在尝试列出某个项目的所有'提交'(这些基本上都是简单的编辑)。这当前正在工作,但是当我尝试列出创建提交的用户时,它显示错误的用户个人资料图片(或名称等)。发生的事情是创建项目的人当前也是所有提交的user_id。

即,用户A创建项目...然后用户B来并添加提交。但由于某种原因,它将用户A显示为所有提交的个人资料照片,我无法弄清楚原因。这必须与提交的“新”或“创建”方法有关,因为它总是将新提交与创建项目的用户相关联(当它应该将提交与current_user关联时)。

提前感谢您的帮助......

COMMITS CONTROLLER

class CommitsController < ApplicationController
  before_action :find_project

  def new
    @commit = @project.commits.build(user_id: @project.user_id) 
  end

  def show
    @project = Project.find(params[:project_id])
    @commit = Commit.find(params[:id])
  end

  def index
    # @user = User.where(:id => @commit.user_id) first figure out how to set user_id on new commits (its nil now)
    @commits = Commit.paginate(page: params[:page])
  end

  def create
    @project = Project.find(params[:project_id])
    @commit = @project.commits.build(commit_params)
    if @commit.save
      flash[:success] = "You've successfully committed to 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 commit_params
      params.require(:commit).permit(:title, :project_id, :user_id)
    end
end

NEW.HTML.ERB(新委托表格)

<div class="row-fluid">
  <div class="col-md-5 no-pad">
    <h1>Commit a new session file to this project repository</h1>
    <%= bootstrap_form_for @commit, :url => project_commits_path do |f| %>

      <%= render 'shared/error_messages', object: f.object %>

      <%= f.text_field :title %>

      <%= f.hidden_field :user_id %>

      <div class="well">
        <h4>Drag and Drop a file here:</h4>
        <%= image_tag("wavicon-large-grey.png", alt: "add file") %>
      </div>

      <%= f.button "Commit Now! ", class: "btn btn-lg btn-primary" %>
    <% end %>
  </div>
  <div class="col-md-1">
    <%= image_tag "shadow-vert.png" %>
  </div>

</div>

INDEX.HTML.ERB(显示错误的用户个人资料照片的项目提交列表)

<% provide(:title, @commits ) %>

<div class="row-fluid">
  <section class="no-pad col-md-9">

    <%= render :partial => '/projects/project_header' %>

    <h4>Commit History</h4>

    <%= will_paginate %>

    <ul class="commits">

      <% @project.commits.first(5).each do |commit| %> 

        <%= render partial: "commit", object: commit %>

      <% end %>
    </ul>

    <%= will_paginate %>

  </section>

</div>

_COMMIT.HTML.ERB PARTIAL(上面引用)

<li>
  <%= link_to project_commit_path(@project, commit) do %>
    <h6><%= image_tag commit.user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %><%= commit.title %>...</h6>
    <span class="timestamp pull-right">
      Created <%= time_ago_in_words(commit.created_at) %> ago
      <span class="glyphicon glyphicon-time"></span>
    </span>

  <% end %>
</li>

我哪里错了?下面的代码应该显示创建“提交”的用户...而不是创建“项目”的用户。

<h6><%= image_tag commit.user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %><%= commit.title %>...</h6>

1 个答案:

答案 0 :(得分:1)

更新new操作,如下所示:

  def new
    @commit = @project.commits.build(user_id: current_user.id) 
  end

使用current_user.id代替@project.user_id。这样,new提交记录将使用当前登录的用户ID 创建,而不是创建项目的人员的用户ID。