# How can I put that code in one line?

**URL:** <https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984>\
**Category:** Rails\
**Created:** [March 26, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984 "2008-03-26T13:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 26, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984/1 "2008-03-26T13:31:48Z")

</div>

I have this checkbox. How can I put that code in one line?

\<% if @repair\_ticket.send\_email == 1%\>  
\<%= check\_box\_tag “send\_email”, @repair\_ticket.send\_email, 1 , {  
:disabled =\> true } %\>  
\<% end %\>  
\<% if @repair\_ticket.send\_email == 0%\>  
\<%= check\_box\_tag “send\_email”, @repair\_ticket.send\_email, checked =  
false , { :disabled =\> true } %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 26, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984/2 "2008-03-26T14:49:53Z")

</div>

Try:  
\<%= check\_box\_tag “send\_email”, @repair\_ticket.send\_email,  
@repair\_ticket.send\_email, {:disabled =\> true } %\>

If @repair\_ticket.send\_email was set as a boolean value in a  
migration, ROR should interpret the send\_email field as true or false  
and thus correctly check/uncheck the checkbox.

-Chris

On Mar 26, 8:31 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 26, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984/3 "2008-03-26T15:00:43Z")

</div>

On 26 Mar 2008, at 13:49, Christopher Hazlett wrote:

> Try:  
> \<%= check\_box\_tag “send\_email”, @repair\_ticket.send\_email,  
> @repair\_ticket.send\_email, {:disabled =\> true } %\>
> 
> If @repair\_ticket.send\_email was set as a boolean value in a  
> migration, ROR should interpret the send\_email field as true or false  
> and thus correctly check/uncheck the checkbox.

or even

\<%= check\_box ‘repair\_ticket’, ‘send\_email’, :disabled =\> true %\>  
(but that will result in a different parameter name)

Fred

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 26, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/how-can-i-put-that-code-in-one-line/132984/4 "2008-03-26T15:11:32Z")

</div>

If you move the some of it to the helper, you can do this

## HELPER

def send\_email\_checkbox(send\_email)  
if send\_email == 1  
value = “1”  
elsif send\_email == 0  
value = “checked = false”  
end

```
return check_box_tag 'send_email', @repair_ticket.send_email,

```

value, :disabled =\> true  
end

## VIEW

\<%= send\_email\_checkbox(@repair\_ticket.send\_email) %\>

- Brad

On Mar 26, 8:31 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
