创建新记录时存储了错误的 user_id [英] wrong user_id being stored when creating a new record

查看:52
本文介绍了创建新记录时存储了错误的 user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

即,用户 A 创建项目...然后用户 B 来添加提交.但出于某种原因,它显示用户 A 作为所有提交的个人资料图片,我不太明白为什么.这必须与提交的新"或创建"方法有关,因为它总是将新提交与创建项目的用户相关联(当它应该将提交与 current_user 相关联时).

在此先感谢您的帮助...

提交控制器

class CommitsController <应用控制器before_action :find_project定义新@commit = @project.commits.build(user_id: @project.user_id)结尾高清秀@project = Project.find(params[:project_id])@commit = Commit.find(params[:id])结尾定义索引# @user = User.where(:id => @commit.user_id) 首先弄清楚如何在新提交时设置 user_id(现在为零)@commits = Commit.paginate(page: params[:page])结尾定义创建@project = Project.find(params[:project_id])@commit = @project.commits.build(commit_params)如果@commit.saveflash[:success] = "您已成功提交此项目"redirect_to project_path(@project)别的呈现新"结尾结尾定义编辑结尾定义更新结尾销毁结尾私人的def find_project@project = Project.find(params[:project_id])结尾def commit_paramsparams.require(:commit).permit(:title,:project_id,:user_id)结尾结尾

NEW.HTML.ERB(新提交表单)

<div class="col-md-5 no-pad"><h1>提交一个新的会话文件到这个项目仓库</h1><%= bootstrap_form_for @commit, :url =>project_commits_path 做 |f|%><%= render 'shared/error_messages', object: f.object %><%= f.text_field :title %><%= f.hidden_​​field :user_id %><div class="well"><h4>将文件拖放到此处:</h4><%= image_tag("wavicon-large-grey.png", alt: "添加文件") %>

<%= f.button "Commit Now!", class: "btn btn-lg btn-primary" %><%结束%>

<div class="col-md-1"><%= image_tag "shadow-vert.png" %>

INDEX.HTML.ERB(项目提交列表显示不正确的用户个人资料图片)

<% provide(:title, @commits) %><div class="row-fluid"><section class="no-pad col-md-9"><%=渲染:部分=>'/projects/project_header' %><h4>提交历史</h4><%= will_paginate %><ul class="commits"><% @project.commits.first(5).each do |commit|%><%= 渲染部分:提交",对象:提交 %><%结束%><%= will_paginate %></节>

_COMMIT.HTML.ERB PARTIAL(参考上文)

  • <%= 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">创建 <%= time_ago_in_words(commit.created_at) %>前<span class="glyphicon glyphicon-time"></span></span><%结束%>
  • 我哪里出错了?下面的代码应该显示创建提交"的用户......而不是创建项目"的用户.

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

    解决方案

    更新new动作如下:

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

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

    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>
    

    解决方案

    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.

    这篇关于创建新记录时存储了错误的 user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    其他开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆