Aakanksha
Aakanksha

Reputation: 976

Record get saved in russian instead of english while using I18n with Rails

i am pretty new to ROR and i'm currently trying I18n. I have a dropdown of payment options along with other details where a user would order something. Also, i have a static array from which the user will be able to select, defined in model

PAY_MODE = [:cheque, :credit_card, :purchase_order]

My locale file is

order:
 back_to_product: "назад"
 sent_email: "Поздравления. Ваш заказ был успешным размещены ."
 PAY_MODE:
    cheque: "чек об оплате"
    credit_card: "Кредитная карта"
    purchase_order: "Заказ на покупку"
    select_prompt: "Выберите режим оплаты"

Everything works perfectlty, however while saving the same into the database, i get the error

undefined method `translation missing: en.cheque' for #Order:0x007fc0e00fc898>

The controller looks like

def create
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        OrderNotifier.received(@order).deliver
        format.html { redirect_to store_index_path, notice: t('order.sent_email') }
        format.json { render action: 'show', status: :created, location: @order }
      else
        format.html { render action: 'new' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

private

def order_params
      params.require(:order).permit(:name, :address, :email, :pay_type)
    end

and i'm displaying the options in view through

<%= f.select :pay_type, t(Order::PAY_MODE, scope: 'order.PAY_MODE'),prompt: t('order.PAY_MODE.select_prompt') %>

i know, i'll have to search its corresponding english translation but how do i do that. It searches it in the en.cheque instead of en.order.PAY_MODE

Any help is appreciated. Thanx in advance.

UPDATE:

So, i solved my initial problem.Apparently, in model, i did not write validation in proper format. It should have been

PAY_MODE = [:cheque, :credit_card, :purchase_order]
validates :pay_type, :inclusion => { :in => I18n.t(PAY_MODE, scope: 'order.PAY_MODE'), :message => "is not a valid" }

and now the record is getting saved into the DB. However, now the problem is that it gets saved in Russain language, rather that english. What do i do to save it to english?

Upvotes: 1

Views: 86

Answers (1)

huyhoang-vn
huyhoang-vn

Reputation: 335

I see you use t(Order::PAY_MODE, scope: 'order.PAY_MODE') is not good. Scope option in t method can't use with a array. Let see http://guides.rubyonrails.org/i18n.html (4.1 part).
You can write same that:

f.select :pay_type, t('order.PAY_MODE').to_h, prompt:....

Upvotes: 0

Related Questions