Ruby on rails 4 未找到参数:在rails4强参数中
使用rails 4强参数时出错Ruby on rails 4 未找到参数:在rails4强参数中,ruby-on-rails-4,strong-parameters,Ruby On Rails 4,Strong Parameters,使用rails 4强参数时出错 ActionController::ParameterMissing at /questions param not found: question 我有一个问题模型和问题控制器。问题表有内容列 这是问题控制器 class QuestionsController < ApplicationController def index @question = Question.new(question_params) @questioner
ActionController::ParameterMissing at /questions
param not found: question
我有一个问题模型
和问题控制器
。问题表有内容列
这是问题控制器
class QuestionsController < ApplicationController
def index
@question = Question.new(question_params)
@questioner = Questioner.new(questioner_params)
end
def new
@question = Question.new(question_params)
end
def edit
@question = find(params[:id])
raise "Question Not edited!" unless @question
end
def create
@question = Question.new(question_params)
respond_to do |wants|
if @question.save
flash[:notice] = 'You have successfully posted the questions!'
wants.html { redirect_to(root_path) }
wants.xml { render :xml => @question, :status => :created, :location => @question }
else
flash[:error] = "Please review the problems below."
wants.html { redirect_to(questions_path) }
wants.xml { render :xml => @question.errors, :status => :unprocessable_entity }
end
end
end
private
def question_params
params.require(:question).permit(:content)
end
end
类问题控制器@question,:status=>:created,:location=>@question}
其他的
flash[:error]=“请检查以下问题。”
wants.html{重定向到(问题路径)}
wants.xml{render:xml=>@question.errors,:status=>:unprocessable_entity}
结束
结束
结束
私有的
定义问题参数
参数要求(:问题)。允许(:内容)
结束
结束
问题在于控制器的索引操作:
def index
@question = Question.new(question_params)
@questioner = Questioner.new(questioner_params)
end
首先,没有提问者参数
。更重要的是,索引操作没有参数-查看rake routes
的输出,尝试定义@questions=Question.all
而不是@Question=Question.new
,因为索引用于显示所有问题的集合
也请查看此内容,它是一本很好的读物,在您刚刚开始时非常有用:
http://guides.rubyonrails.org/getting_started.html
展览在哪里?这在问题控制器中似乎非常重要 试一试 def显示
@question=question.find(参数[:id])
结束
您使用的是提问者参数,但没有在任何地方定义。此外,在显示索引操作时,没有设置任何参数。您只需要在用户单击submit(提交)时输入参数,提交操作应转到create(创建)操作
def index
@question = Question.new
@questioner = Questioner.new
end
我在索引页上有两张表格。一个是使用问题,另一个是提问。我很感激你的回答,但是show action只用于显示单个问题的更多细节。但是现在我得到了未定义的方法'question'错误。请检查并尽可能提供帮助。