Home > Software design >  How to switch API with a model file on Rails
How to switch API with a model file on Rails

Time:11-06

I'm creating a movie reviews website and setting multi languages translation. Because of the translation, Movie API is needed to be switched to each language. Now the API is defined on a model file though, I don't have any ideas that switching API.

Movie model

class Movie < ApplicationRecord
  include HTTParty
  self.primary_key = "id"
  has_many :comments, dependent: :destroy
  has_many :users, through: :comments
  

  default_options.update(verify: false)
  default_params api_key: '#API key', language: "en-EN" #swtiching needed for other languages.                 
  format :json
 ~
end

Firstly, I tried using if and request.path.include? to place URL with English or other languages. But request command is not allowed to use on a model file. How can I switching it? Any solutions?

Thanks.

CodePudding user response:

You should create a method in your Model that accepts language as a parameter, and call that method from the Controller.

  • Related