rails显示复选框值已选中

时间:2015-11-06 08:24:12

标签: ruby-on-rails ruby

如果branch_id的值包含user_branches,我希望def edit @user_branches = @user.user_branches @branch = Branch.all @user = User.find(params[:id]) respond_to do |format| format.js end end 显示已检查的值。我有这样的用户控制器:

<% @branch.each do |branch| %>
  <ul>
    <%= check_box_tag 'branch_ids[]', branch.id, (:checked if @user_branches.map{|x|x.branch_id}.include?(branch.id)) %>
    <%= branch.name -%>
  </ul>
<% end %>

我有这样的观点:

url(r'^testall/$',views.DataList.as_view())

以上代码不起作用。

1 个答案:

答案 0 :(得分:1)

你能试试吗?

 window.onload = function(){
   "use strict";
function MenuItem(name, class, target, id)
{
    this.object = document.getElementsByClassName(class).item(id);
    this.name = name;
    this.class = class;
    this.target = target;
    this.id = id;
    this.is = false;
}

MenuItem.prototype.spec = function(){
    if(window.location == this.object.getAttribute("href"))
    {
        this.object.style.backgroundColor = "rgb(0,156,128)";
        this.is = true;
    }
}

MenuItem.prototype.action = function()
{
    if(!this.is)
    {
        this.object.onmouseover = function(obj){
            obj.style.backgroundColor = "grey";
        }

        this.object.onmouseout = function(obj){
            obj.style.backgroundColor = "transparent";
        }
    }
}

var Home = new MenuItem("Home", "favnav", "link to a page in my site", 0);
var Posts =  new MenuItem("Posts", "favnav", "link to a page in my site", 1);
var Managers =  new MenuItem("Managers", "favnav", "link to a page in my site", 2);
var Routines =  new MenuItem("Routines", "favnav", "link to a page in my site",3);
var About = new MenuItem("About", "favnav", "link to a page in my site", 4)
var Careers = new MenuItem("Authors", "favnav", "link to a page in my site", 5);

var elems = document.getElementsByClassName("favnav");

for(var i = 0; i < elems.length; ++i)
{
    elems.item(i).spec();
    elems.item(i).action();
}

}