0

I am working on a new view for the app we're using (Rails 4.1.6 on Ruby 2.0.0). The model has an integer attribute, max_operations. This is a limit we set and then use.

It can be either a natural number or -9, which we we to represent no limit. On the _form.html.erb, I want it to look something like this:

[...]

max operations

⃝ unlimited

⃝ limit: □ (tiny square to represent a text box)

[...]

If the unlimited option is selected, the value passed for the max_operations would be -9 and if the limit option is selected, the number typed in the adjacent text box would be passed.

I also want the field to be properly shown when editing the record (selecting the proper radio, value shown inside the text box if needed).

I'm not sure where to begin with. Trying to imitate some code examples were a complete failure. I did manage to produce this, which is probably the ugliest piece of code I've written in the last 15 years:

<input type="radio" id="operation_date_max_operations_unlimited" name="operation_date[max_operations]" <%= @operation_date.max_operations==-9?"checked=checked":"" %> value="-9"> <%=t("unlimited")%>
<br>
<input type="radio" id="operation_date_max_operations_limited" name="operation_date[max_operations]" <%= @operation_date.max_operations==-9?"":"checked=checked" %> value=<%= @operation_date.max_operations%> ><%=t("max_operations")%>:
<input type="text" id="max_operations_limit" name="operation_date[max_operations]" value=<%= @operation_date.max_operations==-9?"":@operation_date.max_operations %> >

it does show the things well but changing values does not work when saving, so I'd be glad to re-write the whole thing in a neat way.

I would like to keep it as much as I can within the erb code (<%=f.radio_button etc.), the less js the better. Any ideas?

1
  • At which point are you failing? Have you managed to add the radio buttons and the text field to your form? Commented Mar 18, 2015 at 14:13

1 Answer 1

1

First of all, having a special number to represent something is generally not a good idea.

That said, in order to stick with this approach but also make it more "neat", you can have a method in your OperationDate model that tells you if there is a limit or not:

def limited_operations?
  (max_operations == -9)? true : false
end

Then, in your view, you can start bringing the radio buttons to your needs with the following:

<%= f.radio_button :max_operations, -9, { :checked => @operation_date.limited_operations? } %>
<%= f.radio_button :max_operations, @operation_date.max_operations, { :checked => @operation_date.limited_operations? } %>
<%= f.text_field :max_operations, :value => @operation_date.limited_operations? ? @operation_date.max_operations : "" %>

You can already see a danger here: The text field allows for any kind of values to be given..

Sign up to request clarification or add additional context in comments.

5 Comments

well, I have done so. this resulted with passing nil instead of -9 when selecting the "unlimited" radio.
also, there is validator in model to ensure its value. I expect the form to ignore the value in the text box once I select the ‘unlimited’ radio, which does not happen. I think it’s just assigning the value in the box, regardless of the radio selection.
@Spätzle Have you tried changing the order of the buttons to see what happens?
yep, that didn't change anything. it anyway passes the text box value.
have you got any ideas?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.