Search code examples
ruby-on-railslocalizationlocale

rails mixes id and locale post params


My trouble is that i have 2 controllers (articles & news) , when i edit articles my updates are saved to db. but when i try to do the same with news i get this error

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  

and i didn't see any difference between these two actions in the code
routes

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

Please help!
Many thanks in advance!

Update 1
The error seems to be in routing ( because this is the place that throws error

 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

Update 2
And this is dump of params when error occurs. no field for id here

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

Update 3
tried to set scope '(:locale)', :locale => /en|ru/ do in routes as in here http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params didn't help.

Update 4
the problem is that form_for points to /:id url, which gets overwritten by root route. so changing form_for to form_for(@news, url: news_path(@news)) and have to put here local... searching how to do that


Solution

  • in _form file use

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

    for your form. like so it doesn't get overwritten by root