#..Rails 4 的未定义方法`model_name_question' [英] undefined method `model_name_question' for #..Rails 4

查看:51
本文介绍了#..Rails 4 的未定义方法`model_name_question'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交表单后出现此错误:(在索引页面中)

I am getting this error after submitting the form:(in the index page)

<%= simple_form_for(@quiz, html: {class: 'form-vertical' }) do |f| %>
            <%= render 'shared/error_messages_question' %>
            <%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question."  %>
            <%= f.button :submit %>
<% end %>

我有问题模型:

class Question < ActiveRecord::Base
    validates :question, presence: true

    belongs_to :category
    belongs_to :questioner
end

和问题控制器:

class QuestionsController < ApplicationController
  def index
    @quiz = Question.new
    @questioner = Questioner.new
  end

  def new
    @quiz = Question.new(quiz_params)
  end

  def show
    @quiz = Question.find(params[:id])
  end

  def edit
    @quiz = find(params[:id])
    raise "Question Not edited!" unless @quiz
  end

  def create
    @quiz = Question.new(quiz_params)

    if @quiz.save
      flash[:success] = 'You have successfully posted the questions!'
      redirect_to questions_path
    else
      flash[:error] = "Please review the problems below."
      # render 'new'
      redirect_to questions_path
    end
  end

  private

    def quiz_params
      params.require(:question).permit(:content, :answered, :questioner_id, :category_id)
    end
end

可能是什么问题?在 Rails 服务器中,我有这个:

what could b the problem? in the rails server I have this:

Completed 500 Internal Server Error in 5ms

    NoMethodError - undefined method `question' for #<Question:0x0000000433dfc0>:
      activemodel (4.0.2) lib/active_model/attribute_methods.rb:439:in `method_missing'

推荐答案

该问题可能与此验证行有关

The issue may potentially be related to this validation line

validates :question, presence: true

它假设您的 Question 模型具有 :question 属性.换句话说,确保 questions 数据库表中有一个正确的 question 数据库列.

It assumes your Question model has a :question attribute. In other words, makes sure there is a proper question database column in the questions database table.

如果不是这种情况,请相应地修复表格或验证.

If this is not the case, fix the either the table or the validation accordingly.

这篇关于#..Rails 4 的未定义方法`model_name_question'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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