Вопрос

I currently have a table sorted/filtered by several select boxes and two radio buttons, and it is working fine. I would the select boxes to be automatically populated from a table. Specifically, I would like a state select box that is populated from the States in my Articles table. I currently have an articles.rb model and state.rb model, and I have the relationships set up. article belongs_to :state, state has_many :article. Here is my code so far:

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>

article.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

Please let me know if I need to post any more code or any more information. Anything that could help me would be great.

Это было полезно?

Решение 2

After looking at this website: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

I figured out that I actually did not need to have models set up for State and City. All I needed to do was make a collection_select that selected the Distinct States from my Articles table. Here is what it looks like:

<%= collection_select :article, :state, Article.select(:state).uniq.order('state ASC'), :state, :state, {:prompt => 'Select a State'},{:name => "state_search"} %> 

So all I had to do was set up the collection_select like the website said and add Article.select(:state).uniq.order('state ASC') as my collection to select the uniq states from my Articles table.

Другие советы

Worth taking a look at

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

I think for states you could do something like

<%= collection_select :state_id, State.all, :id, :name, multiple: true, :prompt => "Please Select" %>

Now that may not be exact but its how im doing it in my app and it works (though country rather than state)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top