Babak Bst
Babak Bst

Reputation: 295

How to read radio_button value on rails 3?

I would like to read the selected value of radiobuttons on my Form.Now null value saved in my DB. Here is my radio buttons:

        <%= w.label :Artikel ,'der' %><%= w.radio_button :Artikel %>
        <%= w.label :Artikel ,'die' %><%= w.radio_button :Artikel %>
        <%= w.label :Artikel ,'das' %><%= w.radio_button :Artikel %>

and this is my Controlle:

def create
    @word=Word.create(params[:word])
    if @word.save
      redirect_to :action => 'index'
    else
      render :action => 'new'
    end
  end

Thank you for your helps

Upvotes: 0

Views: 352

Answers (2)

dexter
dexter

Reputation: 13583

The view should have been

<%= w.label :Artikel ,'der' %><%= w.radio_button 'artikel', 'der' %>
<%= w.label :Artikel ,'die' %><%= w.radio_button 'artikel', 'die' %>
<%= w.label :Artikel ,'das' %><%= w.radio_button 'artikel', 'das' %>

I assume an attribute called artikel exists in the Word model.

Upvotes: 1

Dylan Markow
Dylan Markow

Reputation: 124419

You need to give each of your radio_button items a second parameter with the value to use:

<%= w.radio_button :Artikel, "der" %>
<%= w.radio_button :Artikel, "die" %>
<%= w.radio_button :Artikel, "das" %>

Then, params[:word][:Artikel] will contain whichever one is selected.

Upvotes: 1

Related Questions