wrong user_id being stored when creating a new record

随声附和 提交于 2019-12-08 10:49:13

问题


So just got some help on this, but now have a new issue...I'm trying to list all of the 'commits' (these are basically simple edits) for a certain project. This is currently working but when I try to list the user who created the commit it is showing the wrong user profile picture (or name, etc). What is happening is the person who created the project is currently also the user_id for all commits.

i.e, User A creates the project...then User B comes and adds a commit. But its showing User A as the profile pic for ALL commits for some reason, and I can't quite figure out why. This must have something to do with the 'new' or 'create' methods for commits because its always associating the new commit with the user who created the project (when it should be associating the commit with the current_user).

Thanks in advance for any help...

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 (NEW COMMITS FORM)

<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 (list of project commits showing incorrect user profile pic)

<% 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 (referenced above)

<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>

Where am I going wrong? This code below should be showing the user who created the 'commit'...not the user who created the 'project'.

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

回答1:


Update the new action as below:

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

Use current_user.id instead of @project.user_id. This way the new commit record would be created with currently logged in user's id instead of the user id of person who created the project.



来源:https://stackoverflow.com/questions/23376896/wrong-user-id-being-stored-when-creating-a-new-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!