【问题标题】:undefined method `model_name' for User:ClassUser:Class 的未定义方法“model_name”
【发布时间】:2015-11-27 21:14:04
【问题描述】:

我在 ruby​​ on rails 上遇到 simple_form gem 的问题。我正在使用一个实例变量,但是当我在部分声明它时,它出现“WelcomePageController:Class 的未定义方法‘model_name’”我不知道发生了什么。我试图解决它,但它对我来说是不可能的

这是我的控制器页面

class WelcomePageController < ApplicationController

 def index
 end

 def new
    @neword = WelcomePageController.new
 end

 def create
 @neword = WelcomePageController.new(words_params)  
 end

 private

 def words_params
  params.require(:words_input).permit(:word, :definition, :example)
 end

end

这是我的部分表格

<%= simple_form_for @neword do |f| %>

<%= f. input :word, label: "insert a word"%>

<%= f. text :definition, label: "give a definition" %>

<%= f.text :example, label: "give an exmaple" %>

<%= f. button :submit %>

<% end %>

这是我的新观点

<h1>hi, again</h1>

<%= render 'form' %>

希望你能帮帮我:)

【问题讨论】:

  • 你传递给simple_form_for的对象应该是一个模型,你定义的实例变量是一个控制器。

标签: ruby-on-rails ruby methods undefined simple-form


【解决方案1】:

看起来@neword 应该是 ActiveRecord 的实例(例如模型),但您提供的是 WelcomePageController.new 的实例(控制器)。你有任何数据库表吗?然后你应该有一个同名的模型类(例如“Word”),你应该实例化它而不是控制器

@neword = Word.create

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多