开发者

How to read radio_button value on rails 3?

开发者 https://www.devze.com 2023-03-31 07:26 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号