Trt Trt
Trt Trt

Reputation: 5532

Rails get radio_button value

I am trying to fetch a radio button's value from a form:

<%= form_for Text.new, remote: true do |f| %>
<%= f.radio_button :is_code, false %>
<%= f.text_area(:content, :size => '50x10', :class=>'input_text')%>
<%= f.submit "Send", :class=>'input_send' %>
<% end %>

Now when I check my parameters in my controller:

  def create
    response.headers["Content-Type"] = "text/javascript"
    attributes = params.require(:text).permit(:content, :name, :is_code)
    attributes[:name]=current_user.name
    Rails.logger.info attributes[:is_code]
    attributes[:content]='code was given' if attributes[:is_code]==true
    @message = Text.create(attributes)
  end

Now if the radio button is NOT clicked the value I get is null. ( I specified a default value so why this gives me null?)

If the radio button is clicked, Rails logger gives me false.

It is toggling between null/false, instead of false/true. Any ideas?

Upvotes: 1

Views: 1467

Answers (1)

MurifoX
MurifoX

Reputation: 15089

As the documentation states, the third parameter is the value of the HTML object. http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-radio_button

So, i believe you have to set it to true, like this:

<%= f.radio_button :is_code, true %>

Then, if it is not checked, you get the false/null value, as oposed of checking the radio, you will get the true value.

Upvotes: 1

Related Questions