如何让单选按钮的答案在ruby on rails中显示在视图中



我对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 %>

最新更新