2013-04-20 104 views
2

我想在我的Rails表單中不接受<option value="">Select one</option>Rails表單選擇驗證

這是我的代碼:

<div class="field"> 
    <p><%= f.label :category, "Category:" %></p> 
    <%= f.select(:category, [["-- Select One --", "false"],'Analytics','Commerce','Content Management','Gaming','Green','Media','Social Media','Technology - Software','Technology - Hardware', 'Web']) %> 
</div> 

使用jQuery驗證插件:

$(document).ready(function(){ 
    $("input[name='commit']").click(function(){ 
    $("#new_startup").validate({ 
     rules: { startup[category]: { valueNotEquals: "false" } }, 
     messages: { startup[category]: { valueNotEquals: "Please select one" } } 
    }); 
    if ($("#new_startup").valid()) 
     location.href = "#"; 
     $("#tag").css("display","none"); 
     $("#thank-you").css("display","block"); 
    }); 
}); 

這不工作,雖然...有沒有更好的方法?

設置["-- Select One --", ""]也沒有工作。

回答

2

我相信你要的是你的選擇promptinclude_blank

<%= f.select(:category, ['Analytics','Commerce','Content Management','Gaming','Green','Media','Social Media','Technology - Software','Technology - Hardware', 'Web'], :include_blank => '-- Select One --') %>

更多請見docs