Mike
Mike

Reputation: 3

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

I'm very new to ruby on rails and haven't been able to get my radio button selections to display in my view. I can see the answers are appearing in the log but I haven't been able to get them to display in the views.

My _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") %>

My show.html.erb view:

<strong>How Order Is Coming Into Office:</strong>
<%= @article.comein %>

My controller:

  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

Upvotes: 0

Views: 1428

Answers (1)

MarsAtomic
MarsAtomic

Reputation: 10673

Although you haven't posted the full code, you're undoubtedly using a form_for helper for your view, similar to the following:

<%= form_for @some_object do |f| %>
  ...
<% end %>

Your selected format means that you need to use model object helpers rather than tag helpers like radio_button_tag. How can you tell the difference between helper types? Tag helpers all carry a _tag suffix. Tag helpers are used within a form_tag, whereas model object helpers are used within a form_for, which is what you're using.

What you should be using is the radio_button helper (as well as the label helper).

Example:

<%= 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 %>

Upvotes: 1

Related Questions