I am following Ryan Bates railscasts I18n Internationalization and hitting a problem/question.
I am trying to set the language in my link, something like this:
http://localhost:3000/en/site/services for english
and
http://localhost:3000/es/site/services for spanish
I am defining this in my routes file here:
routes.rb
scope ":locale" do
get "site/home"
get "site/about_us"
get "site/faq"
get "site/discounts"
get "site/services"
get "site/contact_us"
get "site/admin"
get "site/posts"
get "categories/new_subcategory"
get "categories/edit_subcategory"
end
and I have in my application controller
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
def default_url_options(options = {})
{locale: I18n.locale}
end
And in my views/layouts/application.html.erb
<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Spanish", locale: "es" %>
Now, whenever I try to run rake routes or navigate to the URL I get
C:\www\project>rake routes
rake aborted!
missing :controller
I am fairly new to routes, can someone help me see/explain the problem? Thanks in advance.
I just pasted all the code you posted into a new rails app and it worked. My guess is that you have other routes in your routes.rb file and one of them is invalid. The routes you posted come out like this:
mike@sleepycat:~/projects/testproj$ rake routes
site_home GET /:locale/site/home(.:format) :locale/site#home
site_about_us GET /:locale/site/about_us(.:format) :locale/site#about_us
site_faq GET /:locale/site/faq(.:format) :locale/site#faq
site_discounts GET /:locale/site/discounts(.:format) :locale/site#discounts
site_services GET /:locale/site/services(.:format) :locale/site#services
site_contact_us GET /:locale/site/contact_us(.:format) :locale/site#contact_us
site_admin GET /:locale/site/admin(.:format) :locale/site#admin
site_posts GET /:locale/site/posts(.:format) :locale/site#posts
categories_new_subcategory GET /:locale/categories/new_subcategory(.:format) :locale/categories#new_subcategory
categories_edit_subcategory GET /:locale/categories/edit_subcategory(.:format) :locale/categories#edit_subcategory
While you might be capable of something like that the question is should you. I would strongly suggest reading up on Resource Oriented Architecture if you aren't already familiar with it. I wouldn't suggest bending Rails into strange shapes until you have a good grasp on that. Its the concept that Rails routing is based on and what goes on in routes.rb won't make much sense until you understand that.
There is plenty available on the internet and a good book that clarified things for me is Restful Web services by Leonard Richardson and Sam Ruby. I hope that helps.
resources sites: and then add a bunch of member routes? I don't need the CRUD for my sites controller, only the actions that will be static (home, products, etc). Is that the best practice for that - ruevaughn 2012-04-04 23:35