2014-04-29 4 views
0

이제이 문제에 대한 도움이되었지만 이제는 새로운 문제가 있습니다 ... 모든 '커밋'(기본적으로 간단한 편집)을 나열하려고합니다. 특정 프로젝트. 현재 작동하지만 커밋을 만든 사용자를 나열하려고하면 잘못된 사용자 프로필 그림 (또는 이름 등)이 표시됩니다. 현재 프로젝트를 만든 사람이 현재 모든 커밋에 대한 user_id이기도합니다.새 레코드를 만들 때 잘못된 user_id가 저장됩니다.

즉 사용자 A가 프로젝트를 생성하면 사용자 B가 와서 커밋을 추가합니다. 그러나 User A를 ALL의 프로필 사진으로 표시하는 것은 왠지 커밋하기 때문에 그 이유를 알 수 없습니다. 이것은 커밋에 대한 'new'또는 'create'메소드와 관련이 있어야합니다. 그 이유는 커밋을 항상 current_user와 연관시켜야 할 때 프로젝트를 생성 한 사용자와 항상 새로운 커밋을 연관시키기 때문입니다. 사전에 어떤 도움

감사합니다 ...

가 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 (NEW는 FORM을 범) (잘못된 사용자 프로필 그림을 보여주는 프로젝트 커밋 목록)

PARTIAL

_COMMIT.HTML.ERB는 어디에서 잘못 가고

<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

업데이트 new 조치 :

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

사용 current_user.id 대신 @project.user_id. 이 방법을 사용하면 new 커밋 레코드가 으로 생성되고 프로젝트를 만든 사람의 사용자 아이디 대신 현재 사용자의 아이디가이됩니다.

+0

당신은 생명의 은인 ... 감사합니다! – BB500

+0

다시 도움이 된 것을 기쁘게 생각합니다 :) –

관련 문제