Need to diagnose routes.rb for app to add english/spanish to Rails 3 app

Go To StackoverFlow.com

1

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.

2012-04-04 21:29
by ruevaughn


1

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.

2012-04-04 23:16
by mikewilliamson
You are right, I have other routes and I've actually narrowed it down. I do need to understand restful routing more, I will check out that book. I am also going to read the Ruby on Rails guides on routing again. Would a more acceptable version be:

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

I have gone down the path of a "site" controller before and it can be made to work. I stuck with Rails (instead of mounting a Sinatra app to take care of the static pages or something like that) because I wanted these "static" pages to update-able and I know I will have to deal with I18n issues across the whole site. In the end I had a controller representing the organisation with "about", "contact" and "home" type stuff as properties of a single organisation stored all by itself in the db. Everything else was Rails CRUD. It worked but I am always left wondering if there is a better way - mikewilliamson 2012-04-05 00:07
Awesome, that is exactly my situation, I also want the 'static' pages to be update-able. But I only need the front end to switch languages, not the back end which is why I was trying to wrap the scope around only my front end 'static' pages, if that make sense. Having gone down this road, are there any other tips on this sort of site as far as structure, or any other thing you can think of - ruevaughn 2012-04-05 02:55
I posted a new question, feel free to look at it. http://stackoverflow.com/questions/10036167/best-practices-for-static-pages-in-rails-ap - ruevaughn 2012-04-05 21:22
Ads