rails I18n路由中的参数顺序错误(id为locale,id为nil)

时间:2015-04-29 14:09:09

标签: ruby-on-rails ruby rails-i18n i18n-gem

我想用i18n for rails将外部代码国际化(参见github)。我已阅读Rails Internationalization (I18n) API的指南。翻译文本不是问题,但基础代码似乎在所有情况下都不能正常工作。不幸的是,我不是铁路/红宝石专家。

来自日志:

  

致命 - :ActionView :: Template ::错误(没有路由匹配   {:action =>“edit”,:controller =>“plans”,:format => nil,:id => nil,   :locale => 115}缺少必需的键:[:id]):

所以问题是,id(= 115)的参数作为locale而不是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上找到(sry,我无法发布链接,因为我没有足够的声誉)。 任何建议/帮助都是完美的。

修改 我想我离我更近一点了。也许现在更清楚,为什么它不起作用。 首先是“完整”的堆栈跟踪:

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_urledit_user_url。因为如果我添加一些随机文本作为参数它可以...:

edit_plan_url("de",@vars[:plan])
and in _notification:
edit_user_url("de",@vars[:user].id)

问题是,为什么它有效?有没有办法打印创建的路线?因为在栈跟踪中路由不匹配,因为格式和id是nil。现在我想看到新的路线,以便知道我的随机字符串“de”的位置。

1 个答案:

答案 0 :(得分:0)

Looks like your routes are expecting two params, and ordering it as it comes. There's a way to avoid it by using a named hash into the params passed:

edit_plan_url(id: @vars[:plan].id)

The Rails Automagic will use the symbol to identify the param and avoid the problem.