0

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

看法:

  <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)
4

1 回答 1

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)
于 2012-12-02T23:26:16.810 回答