[RUBY] [Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)

Implementation details

If you implement the search function using Rakuten API, you will get an error saying ** keyword is not valid ** when the search form is searched with blanks or one character. It is quick to count the number of characters in the corresponding action of the controller and make a conditional branch, or control the input form with Javascript, but then you can only limit the number of characters. I made an article about the process of setting validation like a normal model.

About Rakuten Product Search API

[Rakuten product search API] (http://webservice.rakuten.co.jp/api/ichibaitemsearch/)

Implementation of search function using Rakuten API

[Please refer to the article I referred to (I'm sorry to leave it to others ...)] (https://freesworder.net/rakuten-api-rails/) The content of the above article is from creating a new application, but please refer to it according to your situation. I implemented it in the existing controller (searchs_controller.rb) of the existing application, but the flow was as follows. ① Rakuten app registration → Obtain required ID ② Add index action to searchs controller ③ Add routing (url does not have id) ④ Add Gem ⑤ Add rakuten.rb to the initializer folder (and make id an environment variable) ⑥ Edit controller ⑦ Create View (index.html.erb) Once you've done this, you'll partially edit it to set the validation.

Edit (finishes in 20 minutes)

Create a model that does not inherit ActiveRecord and define a class

Create search_form.rb directly under app/models. (The naming is free, so you can change it without any problem.)

models/search_form.rb


class SearchForm
  
  include ActiveModel::Model
   
  attr_accessor :keyword
  #Please change the following validation according to your needs(I haven't finished...)
  VALID_KEYWORD_REGEX = /\A(?:\p{Hiragana}|\p{Katakana}|[---]|[one-龠])+\z/
  validates :keyword, presence: true, length: { in: 2..20 }, format: { with: VALID_KEYWORD_REGEX}
end

** include ActiveModel :: Model ** is a very convenient module that allows you to create model classes that are not directly linked to the DB. [This is an article that I referred to] (https://naokirin.hatenablog.com/entry/2019/02/20/231317) ** attr_accessor ** seems to be used when you want to define the attributes of an object that can be read and written. In Rails, if you create a corresponding Model, you can handle table columns as attributes without explicitly defining attr_accessor, but this time it is a model that is not linked to DB, so I think that it is an image defined in class. I am thinking. (If you recognize it, please comment ...)

Edit controller

searchs_controller.rb


  def index
    #Determining if a search word exists(If there is no word, it feels like a view of only the search form is displayed)
    if params[:keyword]
      #SearchForm model keywords params[:keyword]Store
      items = SearchForm.new(keyword: params[:keyword])
      #Determining if you got stuck in validation(False)
      if items.valid?
        #.search()The keyword stored in items above is stored in items.I will give it by keyword.
        @items = RakutenWebService::Ichiba::Item.search(keyword: items.keyword)
      else
        #What to do if validation is applied
        flash[:danger] = "Message you want to display"
        redirect_to rakuten_search_path    #Arbitrary path
      end
    end
  end

index.html.erb (for reference)

ruby:searchs/index.html.erb


<div class="col-xs-10 col-xs-offset-1">
  <div class="rakuten-search-box">
    <%= form_with url: rakuten_search_path, method: :get, local: true do |f| %>
      <%= f.text_field 'keyword', placeholder: "Enter a search word...", class: "rakuten-search-form" %>
      <%= f.submit "Search", class: "rakuten-search-btn" %>
    <% end %>
  </div>
  <div class="alert">
    <%= flash[:danger] %>
  </div>
  <div class="rakuten-items">
    <% if @items.present? %>
      <% @items.each do |item| %>
        <%= link_to "#{item.url}" do %>
          <section class="item-detail">
            <%= image_tag item["mediumImageUrls"][0], class: "item-image" %>
            <p><%= item.name %></p>
            <p><%= item.price %>Circle</p>
          </section>
        <% end %>
      <% end %>
    <% else %>
      <p>There is no applicable product</p>
    <% end %>
  </div>
</div>

As an aside, I use Javascript to clear flash messages in 6 seconds. With the above, I think that validation was applied at the time of search. If you have an easier way or a practical implementation, please leave a comment.

Recommended Posts

[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of search function
Rails [For beginners] Implementation of comment function
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
Implementation of search function
Rails search function implementation
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails fuzzy search function implementation
Search function using [rails] ransack
Implementation of sequential search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Summary of rails validation (for myself)
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implementation of validation using regular expressions
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Implementation of user authentication function using devise (2)
Try using the Rails API (zip code)
Strict_loading function to suppress the occurrence of N + 1 problem added from rails 6.1
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
Try using the Emotion API from Android
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Ruby On Rails] How to search the contents of params using include?
[Vue.js] Implementation of menu function Vue.js introduction rails6
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
[Order method] Set the order of data in Rails
Validation settings for Ruby on Rails login function
Implementation of Ruby on Rails login function (Session)
ChatWork4j for using the ChatWork API in Java
Implement the star five function using the for statement
[Rails] Implementation of retweet function in SNS application
[API] I tried using the zip code search API
[Rails] Implementation of batch processing using whenever (gem)
Implementation of search function Learning memo (portfolio creation)
Implement search function with Rakuten Books (DVD) API
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[rails] Set validation
[Ruby on Rails] Implementation of validation that works only when the conditions are met
[Rails] Let's dynamically get the threshold of model validation "length check" from table information
Ruby on Rails <2021> Implementation of simple login function (form_with)
I tried using the Server Push function of Servlet 4.0