【发布时间】:2014-04-29 23:09:10
【问题描述】:
所以刚刚得到了一些帮助,但现在有一个新问题......我正在尝试列出某个项目的所有“提交”(这些基本上是简单的编辑)。这目前正在工作,但是当我尝试列出创建提交的用户时,它显示了错误的用户个人资料图片(或名称等)。正在发生的是创建项目的人当前也是所有提交的 user_id。
即,用户 A 创建项目...然后用户 B 来并添加提交。但出于某种原因,它显示用户 A 作为所有提交的个人资料图片,我不太明白为什么。这必须与提交的“新”或“创建”方法有关,因为它总是将新提交与创建项目的用户相关联(当它应该将提交与 current_user 相关联时)。
提前感谢您的帮助...
提交控制器
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>
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 relationship