2016-12-11 5 views
0

私はこのようなアプリケーションを作成しようとしています。ユーザーはプロジェクトを作成し、このプロジェクトに多くのメンバーを招待できます。今、私はprojects_controller.rbのprojects#indexメソッドを使ってユーザーが作成したすべてのプロジェクトを表示しようとします。しかし、ブラウザ、on/projectsリンクでは、エラーはなく、プロジェクトはありません。ここでrails project#indexユーザープロジェクトを表示しません - Many many many associations

def index 
     @user = current_user 
     @projects = @user.projects 
    end 

def create 
     @project = Project.new(project_params) 
     @project.user_id = current_user.id 
     if @project.save 
      flash[:success] = "successfully created project" 
      redirect_to projects_path 
     else 
      render 'new' 
     end 
    end 

モデルである(ユーザー、プロジェクト、会員、招待):

class Invite < ApplicationRecord 
    belongs_to :project 
    belongs_to :sender, :class_name => 'User' 
    belongs_to :recipient, :class_name => 'User' 

    before_create :generate_token 
    before_save :check_user_existence 

    def generate_token 
     self.token = Digest::SHA1.hexdigest([self.project_id, Time.now, rand].join) 
    end 

    def check_user_existence 
     recipient = User.find_by_email(email) 
     if recipient 
      self.recipient_id = recipient.id 
     end 
    end 

end 

class User < ApplicationRecord 
    has_many :memberships 
    has_many :projects, through: :memberships 
    has_many :invitations, :class_name => 'Invite', :foreign_key => 'recipient_id' 
    has_many :sent_invites, :class_name => 'Invite', :foreign_key => 'sender_id' 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

class Project < ApplicationRecord 
    has_many :memberships 
    has_many :users, through: :memberships 
    has_many :invites 
end 

class Membership < ApplicationRecord 
    belongs_to :user 
    belongs_to :project 
end 

そして、ここでここで

は私のコード、

projects_controller.rbです私が表示したい視点ですuser.projects

ActiveRecord::Schema.define(version: 20161211133001) do 

    create_table "invites", force: :cascade do |t| 
    t.string "email" 
    t.integer "project_id" 
    t.integer "sender_id" 
    t.integer "recipient_id" 
    t.string "token" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "memberships", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "user_id" 
    t.integer "project_id" 
    t.index ["project_id"], name: "index_memberships_on_project_id" 
    t.index ["user_id"], name: "index_memberships_on_user_id" 
    end 

    create_table "projects", force: :cascade do |t| 
    t.string "title" 
    t.integer "nb_team" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "user_id" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.index ["email"], name: "index_users_on_email", unique: true 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    end 

end 

ありがとう:

<div class="container"> 
    <h3> All your projects </h3> 
    <% @projects.each do |project| %> 
     <div class="project-card"> 
      <div class="card-title"> 
       <%= link_to project.title, project_path(project) %> 
      </div> 
     </div> 
<% end %> 
</div> 

はたぶん、あなたはスキーマを見てみたいです!

+1

ページに記載されているプロジェクトが見つかりませんか?おそらく現在のユーザーには彼に関連付けられているプロジェクトはありません –

+0

@ArunKumarいいえリストされたプロジェクトはありませんが、projects_controller.rbの作成アクションでは、この行にプロジェクトのuser_idを入力します: '@project.user_id = current_user .id' –

+0

代わりに '@project = current_user.projects.new(project_params)'を使うことができます。これはあなたのために 'user_id'フィールドを設定します。 Btw、あなたの問題点を教えてください。プロジェクトが見つからない場合は、新しいプロジェクトを作成し、それがインデックスページに表示されているかどうかを確認してください。 –

答えて

0

[OK]を私はこれを書くことで、それを修正:

def index 
     @projects = Project.where(:user_id => current_user) 
end 

多分それは助けます!

+1

'@projects = current_user.projects' –