

初めての質問です。  
Ruby on railsで勉強をさせて頂いているのですが  
rails sを行いページを見ようとすると下記のようなエラがー出てしまいます。

解決法をお教え頂ければ幸いです。

  * ruby v2.2.2
  * rails v4.2.5.1

エラー名

[code]

    NoMethodError in ProjectController#show 
    undefined method `users' for #<Project:0x007f9ef37c73a8>
    
[/code]

**app/controllers/project.controller.rb**

[code]

    class ProjectController < ApplicationController
    
      before_action :authenticate_user!, only: [:list]
    
      def index
        @projects = Project.all
      end
    
      def show
        @project = Project.find(params[:id])
        @tasks = @project.tasks.order(:tag)
    
        @joined = false
    
        if !current_user.nil? && !current_user.projects.nil?
          @joined = current_user.projects.include?(@project)
        end
    
        @users = @project.users.order('created_at desc').first(10)
      end
    
      def list    
        if !current_user.nil?
          @projects = current_user.projects
        end
      end
    end
    
[/code]

**app/admin/project.rb**

[code]

    ActiveAdmin.register Project do
      permit_params :name, :content, :price, :image
    
      show do |t|
        attributes_table do
          row :name
          row :content
          row :price
          row :image do
            project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, "No photo yet")
          end
        end
      end
    
      form :html => { :enctype => "multipart/form-data" } do |f|
        f.inputs do
          f.input :name
          f.input :content
          f.input :price  
          f.input :image, hint: f.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
        end
        f.actions
      end
    end
    
[/code]

**app/model/project.rb**

[code]

        class Project < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name, use: [:slugged, :finders]
    
      has_many :tasks
    
      validates :name, presence: true, length: { maximum: 50 }
      validates :content, presence: true, length: { maximum: 500 }
      validates :price, presence: true, numericality: { only_integer: true }
    
      has_attached_file :image, :styles => { :medium => "680x300>", :thumb => "170x75>" }
      validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
    
      def shortname
        name.length > 25? name[0..25] + "..." : name
      end
    end
    
[/code]

app/project/views/project/show.html.rb


CC BY-SA 3.0
2016-02-25T05:01:21.190
22509
2016-10-18T03:10:39.030
2016-02-25T05:45:57.800
76
14544
question
ruby-on-rails ruby
NoMethodError undefined method のエラーが出る


おそらく、`app/model/project.rb`に

[code]

    has_many :users
    
[/code]

が不足しているのでしょう。

`belongs_to`や`has_many`などの[アソシエーション](http://railsguides.jp/association_basics.html)の意味や使い方について確認されると良いと思います。


CC BY-SA 3.0
2016-02-29T01:26:35.550
22648
2016-02-29T02:51:56.023
2016-02-29T02:51:56.023
76
5793
22509
answer