【问题标题】:How to get radio button answers to show in view in ruby on rails如何获取单选按钮答案以在 ruby​​ on rails 中显示
【发布时间】:2016-08-09 14:26:07
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 radio-button


    【解决方案1】:

    虽然您没有发布完整的代码,但毫无疑问,您使用的是 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 %>
    

    【讨论】:

    • @Mike 很高兴事情为你解决了。如果愿意,请单击答案左上角的复选标记,让其他人知道什么对您有用。这个答案可能对其他人和对您一样有用。
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多