根据下拉菜单选择查询数据库

时间:2012-12-02 22:56:24

标签: javascript jquery ruby-on-rails

如何根据下拉菜单选择查询我的数据库?

查看:

  <div class="field">
    <label>Category</label><br />

    <select name="category[id]" id="category_id">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
  </div>

控制器:

   s = params[category_id]
   @users = User.where("status = ?", s)

1 个答案:

答案 0 :(得分:0)

假设您通过GET或POST正确提交表单数据,则需要对代码进行以下更改

s = params[:category][:id] # <-- category[id] as a form field name is referenced like this through params
@users = User.where("status = ?", s)