Home>

We are creating an application that allows you to search for restaurants based on three search conditions (genre, region, and tag).
Genres/regions are saved as columns in the shop table.
The tag is
shop table-shop_tag_relation intermediate table-tag table
It is saved using an intermediate table like.

NoMethodError in ShopsController # search
undefined method `to_i'for ["", "1"]: Array

If i enter the conditions and press the submit button, the above error will occur.

Corresponding source code

Search form
Since genre and prefecture want to search for a match, use the helper method _eq
Use the helper method _cont because the tag wants to search everything that contains the specified value

# index.html.erb
    <% = search_form_for @p, url: shops_search_path do | f |%>
        
        <% = f.label: genre_eq,'genre'%>
        <% = f.collection_select: genre_id_eq, Genre.all,: id,: name, include_blank:'not specified'%>
        <br>
        
        
        <% = f.label: prefecture_eq,'location'%>
        <% = f.collection_select: prefecture_id_eq, Prefecture.all,: id,: name, include_blank:'not specified'%>
        <br>
        
        
          
          <% = f.label: shop_tag_relations_tag_id_cont,'Commitment condition'%>
          <% = f.collection_check_boxes: shop_tag_relations_tag_id_cont, Tag.all,: id,: name%>
          <br>
          
        
      <% = f.submit'Search'%>
    <% end%>

controller
The strong parameter is set to pass anything temporarily.

class ShopsController
What I tried

I tried: shop_tag_relations_tag_ids_cont to submit the parameters in an array in the search form, but it didn't work ...
I've been addicted to it for a whole day.
By all means, thanks for your support.

Supplementary information (FW/tool version, etc.)

docker environment
rails6
ruby 2.6.5
mysql2

Please provide more detailed information here.

  • Answer # 1

    Has_many: tags in the model Shop, right?
    Then
    f.collection_check_boxes: shop_tag_relations_tag_id_cont, Tag.all,: id,: name%>
    By the way: tag_name_contIt seems to be. Just in case, please try the plural form.