Rails check_box:检查是否有新记录?

时间:2016-02-01 18:56:31

标签: ruby-on-rails forms ruby-on-rails-4

在Rails 4 form_for形式中,我有:

f.check_box :visible

我希望在呈现全新(即未保存)对象的表单时,将复选框呈现为已检查

保存对象时,我希望复选框行为正常(即根据保存对象的值进行检查或不检查)。

这有效:

f.object.new_record? ? (f.check_box :visible, :checked => true) : (f.check_box :visible)

..但它很长!有没有更简单/更好的方法可以实现这个目标?

2 个答案:

答案 0 :(得分:1)

f.check_box :visible, checked: ( f.object.new_record? || f.object.visible )

答案 1 :(得分:0)

在控制器中创建新对象时,只需将属性设置为true即可:

Private Sub Workbook_Open()
Application.ScreenUpdating = False

Dim targ As Range
Dim msg As Range

Set targ = "DETAILS!B6"
Set msg = "DETAILS!B42"

msg.EntireRow.Hidden = True


With Range("DETAILS!B6:B40")
    .EntireRow.Hidden = False

    For Each cell In Range("DETAILS!B6:B40")
    Select Case cell.Value

    Case Is = 0
    cell.EntireRow.Hidden = True

    End Select
    Next cell
End With


If targ.EntireRow.Hidden = True Then
    msg.EntireRow.Hidden = False
End If

Application.ScreenUpdating = True
End Sub