我想用 i18n for rails国际化外部代码(参见github )。我已阅读Rails Internationalization (I18n) API指南。翻译文本不是问题,但底层代码似乎无法在所有情况下正常工作。不幸的是,我不是 rails/ruby 专家。
从日志:
致命 -- : ActionView::Template::Error (没有路由匹配 {:action=>"edit", :controller=>"plans", :format=>nil, :id=>nil, :locale=>115}缺少必需的键:[:id]):
所以问题是,id (=115) 的参数是作为语言环境而不是作为 id 传递的。
为了让 i18n 正常工作,我将以下代码添加到 app/controllers/application_controller.rb 中:
...
before_action :set_locale
protected
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options = {})
{ locale: I18n.locale }.merge options
end
...
此外,我将原始路由包装在 config/routes.rb 中:
Dmptool2::Application.routes.draw do
scope "(:locale)", locale: /en|de/ do
a lot of original routes
end
end
因此,问题是,是否缺少路线或代码内部是否存在问题,或者只是我的错。除了翻译文本和按钮之外,我没有更改原始代码。原始 routes.rb 可以在 github 上找到(抱歉,我无法发布链接,因为我没有足够的声誉)。任何建议/帮助都是完美的。
编辑 我想我更接近一点。也许现在更清楚了,为什么它不起作用。首先是“完整”堆栈跟踪:
F, [2015-05-04T16:43:58.600384 #19289] FATAL -- :
ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"plans", :format=>nil, :id=>nil, :locale=>#<Plan id: 158, name: "asdfe", requirements_template_id: 59, solicitation_identifier: "",
submission_deadline: nil, visibility: :public, created_at: "2015-05-04 14:41:33", updated_at: "2015-05-04 14:43:48", current_plan_state_id: 300>} missing required keys: [:id]):
2:
3: The plan "<%= @vars[:plan].name %>" has been completed.
4:
5: If you have questions pertaining to this action, please visit the DMP Overview page at <%= edit_plan_url(@vars[:plan]) %>
6:
7: <%= render :partial => 'users_mailer/notification_boilerplate.text.erb' %>
app/views/users_mailer/dmp_owners_and_co_committed.text.erb:5:in `_app_views_users_mailer_dmp_owners_and_co_committed_text_erb__754483862330985648_69917281861000'
app/mailers/users_mailer.rb:32:in `notification'
app/models/concerns/plan_email.rb:50:in `block in email_dmp_saved'
app/models/concerns/plan_email.rb:49:in `email_dmp_saved'
app/models/plan_state.rb:31:in `update_current_plan_state'
app/controllers/plan_states_controller.rb:97:in `create_plan_state'
app/controllers/plan_states_controller.rb:73:in `committed'
如果我点击网页上的“完成”按钮,则调用该函数committed
,即调用create_plan_state(:committed)
. 在 create_plan_state 的定义中,有语句plan_state = PlanState.create( plan_id: @plan.id, state: state, user_id: current_user.id)
。这会触发回调after_create: update_current_plan_state
:
def update_current_plan_state
#update the current plan pointer in the plan model
p = self.plan
p.current_plan_state_id = self.id
p.save!
end
现在,这会触发after_save: email_dmp_saved
:
def email_dmp_saved
...
if current_state.state == :committed
users = self.users
users.delete_if {|u| !u[:prefs][:dmp_owners_and_co][:committed]}
users.each do |user|
UsersMailer.notification(
user.email,
"PLAN COMPLETED: #{self.name}",
"dmp_owners_and_co_committed",
{:user => user, :plan => self } ).deliver
end
我认为通知的定义并不重要。但是最后第三行调用“dmp_owners_and_co_committed”,定义为:
Hello <%= @vars[:user].full_name %>,
The plan "<%= @vars[:plan].name %>" has been completed.
If you have questions pertaining to this action, please visit the DMP Overview page at <%= edit_plan_url(@vars[:plan]) %>
<%= render :partial => 'users_mailer/notification_boilerplate.text.erb' %>
在 _notification_boilerplate.text.erb 中有:
You may change your notification preferences at <%= edit_user_url(@vars[:user].id) %>#tab_tab2 .
我认为问题是edit_plan_url
和edit_user_url
。因为如果我添加一些随机文本作为参数它可以工作......:
edit_plan_url("de",@vars[:plan])
and in _notification:
edit_user_url("de",@vars[:user].id)
问题是,它为什么有效?有没有办法打印创建的路线?因为在堆栈跟踪中路由不匹配,因为格式和 id 为 nil。现在我想查看新路线,以了解我的随机字符串“de”的放置位置。