【发布时间】:2017-10-22 05:19:01
【问题描述】:
所以我正在用 Ruby 做一个项目,我想让人们填写一个表格,这将是一个问题的答案,并且能够对同一个问题做出另一个答案。为此,我想使用表单中的数据将答案对象保存到我的数据库中,然后清除表单。我还没有弄清楚如何使用 link_to 标签来做到这一点。
这里是对象控制器:
class AnswersController < ApplicationController
def index
@answers = Answer.all
end
def new
@answer = Answer.new
end
def show
@answer = Answer.find(params[:id])
end
def create
Question.order (:created_at)
@question = Question.last
@answer = @question.answers.build(answer_params)
#if @answer.save
# redirect_to root_url
#else
# render static_pages/home
#end
redirect_to root_url
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def answer_params
params.require(:answer).permit(:content, :correct)
end
end
这是新的.html.erb:
<% provide(:title, 'New Answers') %>
<h1>New Answers</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@answer) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label @content%>
<%= f.text_field @content, class: 'form-control' %>
<%= f.label @correct%>
<input type="checkbox" name="Correct?" value="Correct?" checked> This answer is correct<br>
<%= f.submit "Finish creating Quiz", class: "btn btn-primary" %>
<%= link_to "Create more Questions", createQuestion_path, class: "btn btn-primary" %>
<%= link_to "Create more Answers", createAnswer_path, class: "btn btn-primary" %>
<% end %>
</div>
</div>
任何帮助将不胜感激。如果您需要其他任何内容以进一步了解问题,请告诉我
谢谢!
【问题讨论】:
-
这个问题我不清楚。如果你什么用户在成功创建记录后看到一个空白表单,你为什么不在
if @answer.save分支中直接redirect_to :new呢?
标签: ruby-on-rails ruby database