在 rails 中使用 check_box 接受输入
accepting input using check_box in rails
我是初学者,正在学习 rails 教程。作业包括向页面添加问题(可以编辑和 updated/deleted)。我试图让我的问题表单接受来自 check_box 的关于问题是否得到回答的输入(包括在更新部分)。
我已阅读有关 check_box here and and here 的文档,发现我需要 check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
。但是,我不确定我将在我的表单中的什么地方使用它,以及我将作为 object_name、方法和选项传递的内容。任何指点将不胜感激!
控制器:
class QuestionsController < ApplicationController
def index
@questions = Question.all
end
def new
@question = Question.new
end
def create
@question = Question.new(params.require(:question).permit(:title, :body))
if @question.save
flash[:notice] = "Question was saved."
redirect_to @question
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def show
@question = Question.find(params[:id])
end
def edit
@question = Question.find(params[:id])
end
def update
@question = Question.find(params[:id])
if @question.update_attributes(params.require(:question).permit(:title, :body))
flash[:notice] = "Question was updated"
redirect_to @question
else
flash[:error] = "There was an error saving your post. Please try again."
render :edit
end
end
def destroy
@question = Question.find(params[:id])
@question.destroy
redirect_to questions_path
flash[:notice] = "The question has been deleted."
end
end
显示视图:
<h1><%= @question.title %></h1>
<%= link_to "Edit", edit_question_path(@question), class: 'btn btn-success' %>
<%= link_to "Delete", @question, method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-success' %>
<p><%= @question.body %></p>
编辑视图:
<h1>Edit and Update Question</h1>
<div class="row">
<div class="col-md-4">
<p>Guidelines for questions</p>
<ul>
<li>Stay on topic.</li>
</ul>
</div>
<div class="col-md-8">
<%= form_for @question do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
</div>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
<%= link_to "Delete", @question, method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-success' %>
</div>
<% end %>
</div>
</div>
例如,如果您的 Question
模型中有一个布尔值 answered
属性,那么您在 form_for
块中所要做的就是说 <%= f.check_box :answered %>
。如果选中,它将评估为真值,否则为假。
我是初学者,正在学习 rails 教程。作业包括向页面添加问题(可以编辑和 updated/deleted)。我试图让我的问题表单接受来自 check_box 的关于问题是否得到回答的输入(包括在更新部分)。
我已阅读有关 check_box here and and here 的文档,发现我需要 check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
。但是,我不确定我将在我的表单中的什么地方使用它,以及我将作为 object_name、方法和选项传递的内容。任何指点将不胜感激!
控制器:
class QuestionsController < ApplicationController
def index
@questions = Question.all
end
def new
@question = Question.new
end
def create
@question = Question.new(params.require(:question).permit(:title, :body))
if @question.save
flash[:notice] = "Question was saved."
redirect_to @question
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def show
@question = Question.find(params[:id])
end
def edit
@question = Question.find(params[:id])
end
def update
@question = Question.find(params[:id])
if @question.update_attributes(params.require(:question).permit(:title, :body))
flash[:notice] = "Question was updated"
redirect_to @question
else
flash[:error] = "There was an error saving your post. Please try again."
render :edit
end
end
def destroy
@question = Question.find(params[:id])
@question.destroy
redirect_to questions_path
flash[:notice] = "The question has been deleted."
end
end
显示视图:
<h1><%= @question.title %></h1>
<%= link_to "Edit", edit_question_path(@question), class: 'btn btn-success' %>
<%= link_to "Delete", @question, method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-success' %>
<p><%= @question.body %></p>
编辑视图:
<h1>Edit and Update Question</h1>
<div class="row">
<div class="col-md-4">
<p>Guidelines for questions</p>
<ul>
<li>Stay on topic.</li>
</ul>
</div>
<div class="col-md-8">
<%= form_for @question do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
</div>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
<%= link_to "Delete", @question, method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-success' %>
</div>
<% end %>
</div>
</div>
例如,如果您的 Question
模型中有一个布尔值 answered
属性,那么您在 form_for
块中所要做的就是说 <%= f.check_box :answered %>
。如果选中,它将评估为真值,否则为假。