Question

Im currently working on a form where the user are able to choose between 1-5 radio button alternatives and then upload what they choose to a database.

I'm currently using different name on each radio button and then check which param posted is equal to "on". But since the relationship and the params are described with name I cant get the relationship between the buttons to work (if one button is active the other/s are not active)

So how can I tell which radio button is active if I can't use different names and check for params?

      <div class="form-group">
        <div class="col-lg-10">
        <p style="font-size: 16px">
          <input type="radio" id="option1" name="option1" />
          <label for="option1"><%= @poll.option1 %></label></p>
         </div>
      </div>

      <div class="form-group">
        <div class="col-lg-10">
          <p style="font-size: 16px">
            <input type="radio" id="option2" name="option2" />
            <label for="option2"><%= @poll.option2 %></label></p>
        </div>
      </div>

And this is how I check which on is active, but how can I check if a radio button is active if they dont use different names/params? I'm using Sinatra and Datamapper, SQLite for database management.

if params["option1"] == "on"
  Vote.create(vote: "option1", ip: ip_adress, poll_id: poll.id)
  redirect urlstring
end
if params["option2"] == "on"
  Vote.create(vote: "option2", ip: ip_adress, poll_id: poll.id)
  redirect urlstring
end
Was it helpful?

Solution

You need to specify values for the radio buttons to determine which was submitted - a radio button group will return the value of the selected button.

In view:

<input type="radio" name="options" value="1">Option 1</input>
<input type="radio" name="options" value="2">Option 2</input>
<input type="radio" name="options" value="3">Option 3</input>
<input type="radio" name="options" value="4">Option 4</input>

In controller:

case params["options"].to_i
when 1
  # Vote for 1
when 2
  # Vote for 2
...
end

OTHER TIPS

If i understood correctly,this is what you want

<div class="form-group">
  <div class="col-lg-10">
  <p style="font-size: 16px">
  <label for="poll_option">Poll Option:</label>
    <% ['option1','option2','option3','option4','option5'].each do |poll_option| %>
      <%= radio_button_tag 'poll_option', poll_option,@poll_option == poll_option %>
      <%= poll_option %>
    <% end %>
</div>
</div>

And then,you can use the params like this

if params["poll_option"] == "option1"
  Vote.create(vote: "option1", ip: ip_adress, poll_id: poll.id)
  redirect urlstring
end
if params["poll_option"] == "option2"
  Vote.create(vote: "option2", ip: ip_adress, poll_id: poll.id)
  redirect urlstring
end

Note: Didn't tested though,try it and let me know.

Why are you not using Rails form helpers? If the form is backed by an object, use the form helpers built into rails.

If you are nesting objects, you can use accepts_nested_attributes_for.

Your form can be something like this:

class Poll
  OPTIONS = %w[option_1 option_2 option_3]
end

form_for @poll do |f|
  f.fields_for :vote do |vote|
    Poll::OPTIONS.each do |option|
      vote.radio_button :vote, option

In your controller, you do this:

def new
  @poll = Poll.new
  @vote = @poll.votes.build
end

The create method would be as simple as:

def create
  @poll = Poll.new(poll_params) #if you are using strong_parameters, which you should
  if @poll.save
  else
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top