我试图将我的站立导轨4页国际化。我遵循this基本但旧的指导方针,大部分步骤都运作良好。我遇到的唯一一个大问题是rails在某些页面上混淆了我的参数。
这是一个小例子:
#url: http://localhost:3000/en/list
<% @houses.each do |h| %>
<%= link_to house_path(h) do %>
<% end %>
之前按预期工作,但现在rails无法在params中正确分配所有内容,因此我会收到此错误:
No route matches {:action=>"show", :controller=>"houses", :format=>nil, :id=>nil, :locale=>#<House id: 2, name: "zgerge">} missing required keys: [:id]
显然,rails会混淆语言环境和家庭实例,所以我能够通过更具体的方式解决它:
<% @houses.each do |h| %>
<%= link_to house_path(:id => h) do %>
<% end %>
我的页面中还有一些部分我不知道如何修复它。一个例子是表格:
#url: http://localhost:3000/en/h/1
<%= simple_form_for [@house,@booking] do |f| %>
<% end %>
将返回此错误消息:
No route matches {:action=>"index", :controller=>"bookings", :format=>nil, :house_id=>nil, :id=>"1", :locale=>#<House id: 1, name: "new house">} missing required keys: [:house_id]
现在我正在寻找的是一个更加RESTful的解决方案,然后在错误的每个错误发生时更改查询。
我将在下面提供我的代码的其他部分。如果缺少某些东西,你需要提供帮助,我很乐意添加,所以请告诉我!
#routes
Rails.application.routes.draw do
scope "(:locale)", :locale => /en|de/ do
mount Ckeditor::Engine => '/ckeditor'
root :to => 'pages#home'
get '/impressum', :to => 'pages#impressum', :as => 'impressum'
get '/notes', :to => 'pages#notes', :as => 'notes'
# shortened url for most visited pages
get '/h/:id', :to => 'houses#show', :as => 'house'
put '/h/:id', :to => 'houses#update'
patch '/h/:id', :to => 'houses#update'
delete '/h/:id', :to => 'houses#destroy'
resources :houses, :except => [:show, :update, :destroy] do
resources :pictures
resources :bookings
end
put '/houses', :to => 'houses#activate'
put '/houses/:house_id/pictures', :to => 'pictures#set_default'
put '/houses/:house_id/bookings', :to => 'bookings#change_accepted'
get '/list', :to => 'houses#f_index', :as => 'houses_list'
devise_for :admins
end
end
#application-controller
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Rails.application.routes.default_url_options[:locale]= I18n.locale
end
EDIT1: This stackoverflow thread可能会遇到同样的问题。它已经解决但我不知道如何将其转移到我的问题!