创建新记录时存储错误的user_id
所以刚刚得到了一些帮助,但现在有一个新问题……我正在尝试列出某个项目的所有’提交’(这些基本上都是简单的编辑)。 这当前正在工作,但是当我尝试列出创建提交的用户时,它显示错误的用户个人资料图片(或名称等)。 发生的事情是创建项目的人当前也是所有提交的user_id。
即,用户A创建项目…然后用户B来并添加提交。 但由于某些原因,它将用户A显示为所有提交的个人资料照片,我无法弄清楚原因。 这必须与提交的“新”或“创建”方法有关,因为它总是将新提交与创建项目的用户相关联(当它应该将提交与current_user关联时)。
在此先感谢任何帮助……
COMMITS控制器
class CommitsController @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(新委托表格)
Commit a new session file to this project repository
project_commits_path do |f| %> Drag and Drop a file here:
INDEX.HTML.ERB(项目提交列表显示错误的用户配置文件pic)
'/projects/project_header' %> Commit History
_COMMIT.HTML.ERB PARTIAL(上面引用)
"profile-pic-thumb" %>...
我哪里错了? 下面的代码应该显示创建“提交”的用户…而不是创建“项目”的用户。
"profile-pic-thumb" %>...
更新new
操作如下:
def new @commit = @project.commits.build(user_id: current_user.id) end
使用current_user.id
而不是@project.user_id
。 这样,将使用当前登录用户的id而不是创建项目的人员的用户ID创建new
提交记录。