我目前有一个由几个选择框和两个单选按钮排序/过滤的表格,它工作正常。我会从表中自动填充选择框。具体来说,我想要一个从我的文章表中的州填充的州选择框。我目前有一个articles.rb 模型和state.rb 模型,并且我已经建立了关系。文章属于_to:状态,状态has_many:文章。到目前为止,这是我的代码:
index.html.erb
<%= form_tag articles_path, :method => "GET" do %>
<%= radio_button_tag :gender_search, "M" %>
<%= label_tag :gender_search_M, "M" %>
<%= radio_button_tag :gender_search, "F" %>
<%= label_tag :gender_search_F, "F" %>
<%= select_tag :state_search, options_for_select([['Select a state', 0],['-----------', 0],['LA', 'LA'],['MS', 'MS'], ['TX', 'TX']], @prev_state) %>
<%= select_tag :city_search, options_for_select([['Select a city', 0],['----------', 0],['Mandeville', 'Mandeville'],['Covington', 'Covington']], @prev_city) %>
<%= submit_tag "Go" %>
<% end %>
<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>
</table>
_article.html.erb
<tr>
<td> <%= article_counter +1 %> </td>
<td> <%= article.Title %> </td>
<td> <%= article.Description %> </td>
<td> <%= article.Created_At %> </td>
</tr>
文章.rb
class Article < ActiveRecord::Base
belongs_to :state
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
如果我需要发布更多代码或更多信息,请告诉我。任何可以帮助我的东西都会很棒。