简体   繁体   中英

Using collection_select for Searching In Active Record - Rails 3.2.9

I have only used collection_select once to populate a country id on an associated model User. Now I would like to do a search of User using the same collection_select statement on another view and list the Users for a selected country. My attempts have failed. I can look at the link after a country is selected and see that the id for the country is selected. But when I click my submit button the collection_select statement resets the selected value to the default selected value and ignores the value selected. For example when I select the country of France, the id is 75. When I select France and click Search by Country the id shows up like this.

http://localhost:3000/users_admin?utf8=✓&query=&user%5Bcountry_id%5D=75

Here is the form where I have the collection_select statement. I copied the statement that I am successfully using when I add/update a record on the User model with the selected country_id. What I want my logic to do is when I select a country and click Search by Country that the selected country remains selected in the drop down list and the User records with the selected country_id are displayed on the screen. The Search by Name works as expected.

<%= form_tag users_admin_path, method: 'get' do %>
  <p style="padding-left: 20px;">
    <%= text_field_tag :query, params[:query], placeholder: "Search for first, last or full name" %>&nbsp;&nbsp;
    <span valign="center"><%= submit_tag "Search by Name", class: "btn btn-medium btn-custom", :name => nil %></span>&nbsp;&nbsp;&nbsp;&nbsp;
    <%= collection_select(:user, :country_id, Country.order('name'), :id, :name, {:selected => 233}) %>&nbsp;&nbsp;
    <span valign="center"><%= submit_tag "Search by Country", class: "btn btn-medium btn-custom", :name => nil %>
  </p>
<% end %>

Here is the code in my controller.

def users_admin
  case
  when User.where("active_user = ?", '1').count > 0 # blocked users exist
    @users = User.where("active_user = ?", '1').all
  when params[:commit]=='Search by Country'
    @users = User.where("country_id = ?", params[:country_id]).all
  else
    @users = User.text_search(params[:query])
  end 
  @microposts = Micropost.all
end

I'm not sure if the issue is with how the collection_select statement is coded or another logic problem. My first thought was that I need to somehow save the selected value from the collection_select statement then use it in my where clause. But I do not know how to recode the statement to do that and also have the default selected value as 233 which is the United States when the screen is first displayed. I also thought that maybe I should have two different forms instead of one. I just do not know the direction I should go at this point.

I have searched mainly Stack Overflow for questions regarding this issue. The questions related to collection_select for the most part were relating to uses that are way past what I will probably ever use it for. Again I have only used the collection_statement once:)

Any help will be appreciated.

collection_select should be used when you are working with a model in your form (ie when you use form_for @user do , not form_tag

Try something like this:

<%= select_tag :country_id, options_from_collection_for_select(Country.all, :id, :title, params[:country_id]), include_blank: true %>

The params[:country_id] parameter there makes sure that whatever country_id is passed in via the GET request will be selected in your dropdown after the user submits your search form.

btw, form_for is used when there is a model involved (being created/updated). If you're creating a search form, this isn't the case and you should be using form_tag

Read this for more information: http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

After doing more searching I decided to display params on my view just to see what was in the hash. I did not have commit in my submit_tag so I changed to check to see if params[:country_id] was present. I was able to get the country search working with the modified code:

case
  when User.where("active_user = ?", '1').count > 0 # blocked users exist
    @users = User.where("active_user = ?", '1').all
  when params[:country_id].present?
    @users = User.where("country_id = ?", params[:country_id].to_i).all
  else
    @users = User.text_search(params[:query])
end 

The only problem I have left (minor issue) is that in order to get the name search working properly after a country search the person has to select the blank entry in the country list. Other than that this is solved. It should not take long to figure out how to wipe out the value in params[:country_id]. Either that or I may do an implicit check for params[:query].present? . Thanks for your help. Another pair of eyes did help indeed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM