Project:Class 的未定义方法“model_name" [英] undefined method `model_name' for Project:Class

查看:43
本文介绍了Project:Class 的未定义方法“model_name"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了所有相关问题,但这里对我来说没有什么新鲜事.

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

我收到错误Project:Class 的未定义方法`model_name'"

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

这是一个 erb 文件示例:

This is an erb file sample:

<%= form_tag(@newproject)  do |f| %>
  <%= f.label :name %>:
  <%= f.text_field :description %><br />
<% end %>

推荐答案

如果 Project 不是活动记录子类,您需要这些,您可以使用 form_for

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

查看:

<%= form_for(@newproject)  do |f| %>
  <%= f.label :name %>:
  <%= f.text_field :description %><br />
<% end %>

这篇关于Project:Class 的未定义方法“model_name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆