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' %><%=开发者_Go百科 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
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.
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.
精彩评论