Ruby on rails 选择框对Rails中的表进行排序
我对Rails的世界是相当陌生的,我已经被这个问题困扰了一段时间。我正在为我的框架使用Twitter引导,我正在尝试构建一个基本网站,其中包含一个由选择框排序/过滤的表。我已创建了表,但无法获取用于筛选的选择框。我尝试了几种方法,包括DataTables gem,但都不能正常工作。我不知道我需要用什么才能使这项工作顺利进行Ruby on rails 选择框对Rails中的表进行排序,ruby-on-rails,select,drop-down-menu,Ruby On Rails,Select,Drop Down Menu,我对Rails的世界是相当陌生的,我已经被这个问题困扰了一段时间。我正在为我的框架使用Twitter引导,我正在尝试构建一个基本网站,其中包含一个由选择框排序/过滤的表。我已创建了表,但无法获取用于筛选的选择框。我尝试了几种方法,包括DataTables gem,但都不能正常工作。我不知道我需要用什么才能使这项工作顺利进行 如果你能带我浏览一个基本的网站,其中有一个由选择框排序/过滤的表格,那就太棒了。我根据本教程了解到了这一点,但我必须做一些更改才能使其适用于我,因为我需要选择框。 这是我的
如果你能带我浏览一个基本的网站,其中有一个由选择框排序/过滤的表格,那就太棒了。我根据本教程了解到了这一点,但我必须做一些更改才能使其适用于我,因为我需要选择框。 这是我的新代码: index.html.erb
<form class="form-inline"
<p>
<select name="state_search" class="span2">
<option value="">Select a State</option>
<option>----------------</option>
<option>LA</option>
<option>MS</option>
<option>TX</option>
</select>
<select name="city_search" class="span2">
<option value="">Select a City</option>
<option>----------------</option>
<option>Mandeville</option>
<option>Covington</option>
</select>
<button type="submit" class="btn">GO</button>
</p>
</form>
<table class="table table-striped table-bordered span8 table-condensed"
id="articles_table" >
<thead class="header">
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Created_At</th>
</tr>
</thead>
<tbody>
<%= render @articles %>
</tbody>
<tr>
<td> <%= article_counter +1 %> </td>
<td> <%= article.Title %> </td>
<td> <%= article.Description %> </td>
<td> <%= article.Created_At %> </td>
</tr>
def index
@articles = Article.state_search(params[:state_search]).city_search(params[:city_search]).page(params[:page]).limit(50).order('Created_At DESC')
respond_to do |format|
format.html # index.html.erb
format.json { render json: @articles }
format.js
end
end
def self.city_search(city_search)
if city_search
where('City LIKE ?', "%#{city_search}%")
else
scoped
end
end
def self.state_search(state_search)
if state_search
where('State LIKE ?', "%#{state_search}%")
else
scoped
end
end
文章.rb
<form class="form-inline"
<p>
<select name="state_search" class="span2">
<option value="">Select a State</option>
<option>----------------</option>
<option>LA</option>
<option>MS</option>
<option>TX</option>
</select>
<select name="city_search" class="span2">
<option value="">Select a City</option>
<option>----------------</option>
<option>Mandeville</option>
<option>Covington</option>
</select>
<button type="submit" class="btn">GO</button>
</p>
</form>
<table class="table table-striped table-bordered span8 table-condensed"
id="articles_table" >
<thead class="header">
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Created_At</th>
</tr>
</thead>
<tbody>
<%= render @articles %>
</tbody>
<tr>
<td> <%= article_counter +1 %> </td>
<td> <%= article.Title %> </td>
<td> <%= article.Description %> </td>
<td> <%= article.Created_At %> </td>
</tr>
def index
@articles = Article.state_search(params[:state_search]).city_search(params[:city_search]).page(params[:page]).limit(50).order('Created_At DESC')
respond_to do |format|
format.html # index.html.erb
format.json { render json: @articles }
format.js
end
end
def self.city_search(city_search)
if city_search
where('City LIKE ?', "%#{city_search}%")
else
scoped
end
end
def self.state_search(state_search)
if state_search
where('State LIKE ?', "%#{state_search}%")
else
scoped
end
end
首先,我创建了一个表,它的内容由一个partial呈现,就像在这一集中一样。然后我创建了两个选择框并给它们命名。一个是州搜索,另一个是城市搜索。然后,我进入articles.rb并定义了这些名称。这与railscasts一集中所做的相同,只是我用我的名字替换了这些名字。接下来,我进入articles_controller.rb并将这两个搜索添加到其中,就像railscasts一集中一样。
完成此操作后,两个选择框工作正常,并对表进行了排序/过滤
谢谢到目前为止,您能否发布.erb模板中的内容?您可能需要调整流程。给我们看看你的相关代码。在选择菜单更改时,更新列表并使用ajax重新呈现表。类似的情况下,你希望我发布什么文件?