我正在建立一个关于ROR5的简单问答论坛,我仍然坚持为每个问题提交一个答案。
在相应的问题/:id SHOW视图中,我有一个texteditor,用户可以在其中提交问题的答案。然后答案将发布到问题所在的同一页面。正如您在视图和答案控制器中看到的那样,我尝试通过答案控制器将表单链接到答案模型。
我似乎遇到了路由问题,因为当我点击“提交”时,我得到:
param is missing or the value is empty: question
Started POST "/questions" for 127.0.0.1 at 2017-06-12 15:19:39 +0800
Processing by QuestionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bM4INetkQj94Hy1t64zVLEvIGCumUjLZpA6LwHNT0qTRjYHLYpkpcTw42ulOVNDePsYCAG5puco5AEl+nA6bGw==", "answer"=>{"answercontent"=>"<p>dqwdqwd</p>"}, "commit"=>"Submit"}
Completed 400 Bad Request in 5ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: question):
app/controllers/questions_controller.rb:24:in `question_params'
app/controllers/questions_controller.rb:16:in `create'
如何改进代码以解决此问题?
以下是我的代码的一部分:
<div class="description-wrap">
<div class="description">
<h1>Country:</h1>
<p><%= @question.country %></p>
<h1>Educational Level:</h1>
<p><%= @question.educational_level %></p>
<h1>Topic:</h1>
<p><%= @question.topic %></p>
</div>
<br> <!-- jQuery action to scroll down to text editor -->
<a href=''><button type="button" class="btn">Answer this Question</button></a>
</div>
</div>
<!-- Omitted for brevity -->
<!-- Answers submitted by users will be shown here -->
<%= user.profilepicture_url %>
<%= user.userid %>
<%= answer.answercontent %>
<!-- Submit Answer Form -->
<div class="col-lg-10 col-sm-10 editor">
<%= form_for :answer, url: {action: "create"} do |f| %>
<%= f.text_area :answercontent, :class => "tinymce", :rows => 10, :cols => 90 %>
<%= tinymce %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
如您所见,我在文本区域使用TinyMCE编辑器。这很好。
问题模型有五个参数:
t.string :picture_url
t.string :country
t.string :educational_level
t.string :topic
t.integer :user_id
控制器看起来像这样
class QuestionsController < ApplicationController
def index
@question = Question.all
end
def show
@question = Question.find(params[:id])
end
def new
@question = Question.new
end
def create
@question = Question.new(question_params)
@question.save
redirect_to @question
end
private
def question_params
params.require(:question).permit(:picture_url, :country, :educational_level, :topic)
end
end
答案模型有3个参数:
t.integer :user_id
t.integer :question_id
t.string :answercontent
控制器看起来像这样。
class AnswersController < ApplicationController
def new
# i think something is wrong here too..
@answer = Question.show
end
def create
@answer = Answer.new(answer_params)
render :new
@answer.save
# is this part correct?
redirect_to "/questions/#{question.id}"
end
private
def answer_params
params.require(:question).permit(:user_id, :question_id, :answercontent)
end
end
Rails.application.routes.draw do
root 'home#index'
get 'profile/index' => 'profile#index'
# is this 'post' route correct?
post 'questions/:id' => "answers#create"
resources :users
resources :questions
resources :answers
end
另外,如果我能以任何方式改进这个问题,请发表评论。感谢。
答案 0 :(得分:2)
param丢失或值为空:问题
对于错误,您要将表单提交到questions#create
而不是answers#create
,因此question_params
期望:question
密钥出现在params hash
中,错误也是如此。
您应该编辑如下表格
<%= form_for :answer, url: answers_path do |f| %>
<%= f.text_area :answercontent, :class => "tinymce", :rows => 10, :cols => 90 %>
<%= f.hidden_field :question_id, @question.id %>
<%= tinymce %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
同样在answers_controller
中,您的answer_params
应该是这样的
def answer_params
params.require(:answer).permit(:user_id, :question_id, :answercontent)
end
你的answers#create
错了,它应该在下面
def create
@question = Question.find(params[:answer][:question_id]
@answer = Answer.new(answer_params)
if @answer.save
redirect_to @question
else
render :new #you might have to consider changing this to suite your case
end
end
另外,虽然它与你正在做的事情无关,但answers#new
也是错误的。它应该是
def new
@answer = Answer.new
end