rails混合id和locale post params

时间:2012-11-21 08:43:17

标签: ruby-on-rails localization locale

我的麻烦是我有2个控制器(文章和新闻),当我编辑文章时,我的更新被保存到db。但是当我尝试用新闻做同样的事情时我得到了这个错误

Started PUT "/1" for 127.0.0.1 at 2012-11-21 10:30:17 +0200
Processing by NewsController#index as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OyzVdh0yLz4fGAPwU+tKzuy8B5Wb0exV4+4OjE5UHt8=", "news"=>{"title"=>"fsf", "text"=>"<p>fsdfsfds</p>"}, "commit"=>"Update News", "locale"=>"1"}
1 translation's not available  

我没有看到代码中这两个动作之间有任何区别 路线

scope '(:locale)' do
  get 'articles/autocomplete_article_title'
  resources :users
  resources :news do
  end
  resources :articles do
  end

# match "/news*tail" => 'news#index'
  root :to => 'News#index', as: 'news'
end

请帮忙!
非常感谢提前!

更新1
错误似乎在路由中(因为这是抛出错误的地方

 def set_locale_from_params
    if params[:locale]
      if I18n.available_locales.include?(params[:locale].to_sym)
       I18n.locale = params[:locale]
      else
        flash.now[:notice] =
        "#{params[:locale]} translation\'s not available"
        logger.error flash.now[:notice]
      end
    end
  end

  def default_url_options
    {locale: I18n.locale}
  end

更新2
这是在发生错误时转移参数。这里没有id的字段

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"OyzVdh0yLz4fGAPwU+tKzuy8B5Wb0exV4+4OjE5UHt8=", "news"=>{"title"=>"fsf", "text"=>"<p>fsdfsfds</p>"}, "commit"=>"Update News", "controller"=>"news", "action"=>"index", "locale"=>"1"}

更新3
尝试在路线中设置scope '(:locale)', :locale => /en|ru/ do,因为此处http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params没有帮助。

更新4
问题是form_for指向/:id url,它被root路由覆盖。所以将form_for更改为form_for(@news, url: news_path(@news))并且必须将其放在本地...搜索如何执行此操作

1 个答案:

答案 0 :(得分:0)

在_form文件中使用

 form_for(@news, url: {action: 'show', id: @news, locale: params[:locale]} ) 

表格。像这样它不会被root

覆盖