1

I have a deals controller that has a list of deals, I want each deal to have 3 radio buttons to toggle between "won, lost, pending". What would be the best approach in order to achieve this?

I have a couple of options, either create new entries in the deals table of type boolean, or create a different table connected to the deals table with a foreign key that has a boolean value for each option. I'm a bit confused..

_deal_status.html.erb

<%= form_for @deal do |f| %>

  State:
  <label>
    <%= f.radio_button :state, 'pending', :class => "radio" %>
    Pending
  </label>
  <label>
    <%= f.radio_button :state, 'won', :class => "radio" %>
    Won
  </label>
  <label>
    <%= f.radio_button :state, 'lost', :class => "radio" %>
    Lost
  </label>

<% end %>

this form is resetting the original @deal form to nil except for the radio buttons value.

1
  • If you include the _deal_status.html.erb in your index.html.erb template I'm guessing you iterate over the @deals instance variable using a for- or each-block with a deal-variable. Use render 'deal_status', :deal => deal in the index.html.erb and form_for deal in the _deal_status.html.erb to get the deal in question :)
    – theodorton
    Aug 6, 2013 at 6:50

1 Answer 1

0

Sounds like a state column is what you're looking for. If you add several boolean columns you'll have duplication of data as a deal cannot be in 2 of the given states at any time (won and lost at the same time shouldn't be possible). Look into State Machines. You can do more advanced stuff with a State Machine.

Although I highly recommend it, you don't need a State Machine. You can just add a string column state to your deals-table and use that column to keep track of the state of each deal.

$ rails g migration add_state_to_deals state:string

To get the the radio buttons in your form you can do:

<%= form_for @deal do |f| %>
  ...
  State:
  <label>
    <%= f.radio_button :state, 'pending' %>
    Pending
  </label>
  <label>
    <%= f.radio_button :state, 'won' %>
    Won
  </label>
  <label>
    <%= f.radio_button :state, 'lost' %>
    Lost
  </label>
  ...
<% end %>

And have this validation and default value setter in your model:

class Deal < ActiveRecord::Base
  ...
  validates_inclusion_of :state, in: %w{pending won lost}
  after_initialize :set_default_state, if: :new_record?

  private

  def set_default_state
    state = 'pending'
  end
  ...
end
8
  • and can you use this with form_tag ?
    – Kmelkon
    Aug 5, 2013 at 10:50
  • IS this going to be enough? no need for doing anything in jQuery or the controller ? thanks :)
    – Kmelkon
    Aug 5, 2013 at 11:19
  • @Kmelkon Any reason you want to use form_tag instead of form_for? jQuery won't be necessary, but if you want to have a default value you could do that with JS. Preferable the default value is set in the controller or the model.
    – theodorton
    Aug 5, 2013 at 11:28
  • I have the name of the deal before the form in a list of deals and I want to attach this form to each deal name, that's why .. it worked with the form_for but it's not submitting anything.. can you point me how to set the default value please ?
    – Kmelkon
    Aug 5, 2013 at 11:34
  • You'll need to add a f.submit 'Update state' or something within your form_for. If you want auto-submitting when changing the radio you'll have to look into jQuery. See this: guides.rubyonrails.org/form_helpers.html
    – theodorton
    Aug 5, 2013 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.