NoMethodError undefined method error occurs

Asked 2 years ago, Updated 2 years ago, 46 views

This is my first question.
I am studying at Ruby on rails
When I try to look at the page after doing rails, I get the following error.

I would appreciate it if you could tell me the solution.

  • ruby v2.2.2
  • rails v4.2.5.1

Error Name

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

app/controllers/project.controller.rb

class ProjectController<ApplicationController

  before_action:authenticate_user!, only:[:list]

  def index
    @projects=Project.all
  end

  def show
    @project=Project.find (params[:id])
    @[email protected](:tag)

    @joined=false

    if!current_user.nil?&!current_user.projects.nil?
      @joined=current_user.projects.include?(@project)
    end

    @[email protected]('created_at desc').first(10)
  end

  def list    
    if!current_user.nil?
      @projects=current_user.projects
    end
  end
end

app/admin/project.rb

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

app/model/project.rb

class Project <ActiveRecord::Base
  extend FriendlyId
  friendly_id:name, use:[:slugged,:finders]

  has_many —tasks

  values:name,presence:true,length:{maximum:50}
  values:content, presence:true, length:{maximum:500}
  values:price,presence:true,numberality:{only_integer:true}

  has_attached_file: image, :styles=>{:medium=>"680x300>", :thumb=>"170x75>"}
  values_attachment_content_type: image, :content_type=>/\Aimage\/.*\Z/

  def shortname
    name.length>25?name [0..25]+"...":name
  end
end

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

ruby-on-rails ruby

2022-09-30 12:08

1 Answers

Probably in app/model/project.rb

 has_many:users

is probably missing.

It would be good if you could confirm the meaning and usage of association such as belongs_to and has_many.


2022-09-30 12:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.