我对ruby on rails非常陌生,还没能在视图中显示我的单选按钮选择。我可以看到答案出现在日志中,但我无法将它们显示在视图中。
我的_form.html.erb:
<%= f.label :_ %>
<%= radio_button_tag(:comein, "Drop Off") %>
<%= label_tag(:comein, "Drop Off") %>
<%= radio_button_tag(:comein, "Pick Up") %>
<%= label_tag(:comein, "Pick Up") %>
我的show.html.erb视图:
<strong>How Order Is Coming Into Office:</strong>
<%= @article.comein %>
我的控制器:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
# snippet for brevity
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:number, :address, :forename, :surname, :ordertype, :notes, :comein, :goout)
end
end
尽管您还没有发布完整的代码,但毫无疑问,您正在使用form_for
助手来查看,类似于以下内容:
<%= form_for @some_object do |f| %>
...
<% end %>
您选择的格式意味着您需要使用模型对象辅助对象,而不是像radio_button_tag
这样的标记辅助对象。如何区分辅助对象类型?标记助手都带有一个_tag
后缀。标记辅助对象在form_tag
中使用,而模型对象辅助对象在您正在使用的form_for
中使用。
您应该使用的是radio_button
辅助对象(以及label
辅助对象)。
示例:
<%= f.label :comein, "Pick Up", :value => "true" %><br />
<%= f.radio_button :comein, true%>
<%= f.label :comein, "Drop Up", :value => "false" %><br />
<%= f.radio_button :comein, false, :checked => true %>