Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `model_name' for Project:Class

I've looked through all the related questions but nothing's new for me here.

I have a Project controller with "new" action

class ProjectsController < ApplicationController
  def new
    @newproject = Project.new
  end
end

Project is a simple class, not active record:

class Project
  attr_accessor :name, :description
  def initialize
    @name = ""
    @description = ""
  end
end

I get the error "undefined method `model_name' for Project:Class"

This is an erb file sample:

<%= form_tag(@newproject)  do |f| %>
  <%= f.label :name %>:
  <%= f.text_field :description %><br />
<% end %>
like image 893
Dmitry Dyachkov Avatar asked May 30 '12 20:05

Dmitry Dyachkov


People also ask

What is undefined method?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

What does it mean when you get this error message undefined method user for NILClass?

The Undefined method for nil:NILClass occurs when you attempt to use a formula on a blank datapill. This indicates that the datapill was not provided any value at runtime.

What is an undefined method in Ruby?

The undefined method is also called the NoMethodError exception, and it's the most common error within projects, according to The 2022 Airbrake Error Data Report. It occurs when a receiver (an object) receives a method that does not exist.


2 Answers

if Project is not an active record subclass, you need these and you can use form_for

class Project
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  def persisted?
    false
  end
  ...
end

view:

<%= form_for(@newproject)  do |f| %>
  <%= f.label :name %>:
  <%= f.text_field :description %><br />
<% end %>
like image 123
Viktor Trón Avatar answered Oct 21 '22 03:10

Viktor Trón


class Project < ActiveRecord::Base
like image 31
RadBrad Avatar answered Oct 21 '22 02:10

RadBrad