简体   繁体   English

#.. Rails 4的未定义方法“ model_name_question”

[英]undefined method `model_name_question' for #..Rails 4

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 %>

I have question model: 我有question模型:

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

    belongs_to :category
    belongs_to :questioner
end

and questions controller: 和问题控制器:

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

what could b the problem? 这可能是什么问题? in the rails server I have this: 在Rails服务器上,我有这个:

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

It assumes your Question model has a :question attribute. 假设您的Question模型具有:question属性。 In other words, makes sure there is a proper question database column in the questions database table. 换句话说,请确保questions数据库表中有适当的question数据库列。

If this is not the case, fix the either the table or the validation accordingly. 如果不是这种情况,请相应地修复表或验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM