如何在 rails 上的 ruby 中显示单选按钮答案

How to get radio button answers to show in view in ruby on rails

我是 rails 上的 ruby 的新手,无法让我的单选按钮选择显示在我的视图中。我可以看到答案出现在日志中,但我无法让它们显示在视图中。

我的_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 一样使用 model object helpers rather than tag helpers。您如何区分助手类型之间的区别?标签助手都带有 _tag 后缀。标签助手在 form_tag 中使用,而模型对象助手在 form_for 中使用,这就是您正在使用的。

你应该使用的是 radio_button helper(以及 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 %>