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 %>
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 .
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.
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.
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 %>
class Project < ActiveRecord::Base
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With