问题
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:
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:
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'
回答1:
The issue may potentially be related to this validation line
validates :question, presence: true
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.
来源:https://stackoverflow.com/questions/21101512/undefined-method-model-name-question-for-rails-4