2010-01-07 84 views
0

我想重新呈现之前已经选中的复选框呈现页面。复选框与新的渲染部分

结果,我将它转换为一个整数。

,如果我有下面的代码,我怎么做的复选框选中或未不

check_box("price", "total", {:checked => @checkVal}) where @checkVal is my checked/not value (int) 

回答

2

如果使用check_box,Rails的是假设你有一个有total属性的@price对象。如果是这种情况,您可以通过设置@price.total为true(可能在您的控制器中)来设置复选框。例如:

class UserController < ApplicationController 

    def new 
    @user = User.new 
    @user.terms_of_service = true 
    end 

将导致您的视图中选中复选框:

<%= check_box(:user, :terms_of_service) %> 

如果没有总属性的价格模型,你可能应该使用check_box_tag代替(其中有有关如何设置选中的信息)。

1

如果数据不是从一个表来要使用check_box_tag:

check_box_tag("price", "total", @checkVal)