【问题标题】:undefined method `model_name_question' for #..Rails 4#..Rails 4 的未定义方法“model_name_question”
【发布时间】:2014-01-13 21:15:47
【问题描述】:

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

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

我有question 模型:

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 服务器中,我有这个:

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'

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 strong-parameters


    【解决方案1】:

    问题可能与此验证行有关

    validates :question, presence: true
    

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多