Routing in Rails fails when changing application to Multilingual Application - ruby-on-rails

I am fairly new to Rails and have been unable to figure out what I am doing wrong.
Wondering if someone can help?
I have a Rails application that is working without problems, that is before I started modifications to make it multilingual.
To make it multilingual I have taken the following steps:
I added this to the routing file:
scope "(:locale)", :locale => /en|is/ do
Routing file:
PropertyEvaluator::Application.routes.draw do
scope "(:locale)", :locale => /en|is/ do
root :to => "pages#home"
get 'pages/about'
get 'pages/home'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :searches, :only => [:index, :new, :create]
resources :users, :only => [:index, :new, :create, :edit, :update, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :password_resets, :only => [:new, :create, :edit, :update]
resources :email_activations, :only => [:edit]
resources :roles, :only => [:edit, :update]
end
end
Routing file before modificaions:
PropertyEvaluator::Application.routes.draw do
root :to => "pages#home"
get 'pages/about'
get 'pages/home'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :searches, :only => [:index, :new, :create]
resources :users, :only => [:index, :new, :create, :edit, :update, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :password_resets, :only => [:new, :create, :edit, :update]
resources :email_activations, :only => [:edit]
resources :roles, :only => [:edit, :update]
end
To application_controller.rb I added
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Rails.application.routes.default_url_options[:locale]= I18n.locale
To the view I added:
<%= link_to_unless I18n.locale == :is, "IS", locale: :is %>
|
<%= link_to_unless I18n.locale == :en, "EN", locale: :en %>
Things are working as expected with the original routing file, but when I add the scope to the routing file the problem starts.
I get the following error:
No route matches {:action=>"edit", :controller=>"users", :format=>nil, :id=>nil, :locale=>#<User id: 2, email: "bjarni.sigurdsson#bodeind.is", password_hash: "$2a$10$WzcoB5pES3TXYbWpe7xtB.yKFnqi.dhQgMXOp8/nyKi...", password_salt: "$2a$10$WzcoB5pES3TXYbWpe7xtB.", created_at: "2014-09-30 15:20:38", updated_at: "2014-10-01 12:41:57", name: "Bjarni SigurĂ°sson", auth_token: "wY2cfwk-1R7fSjCvqQPaWQ", password_reset_token: nil, password_reset_sent_at: nil, admin: true, email_confirmed: true, email_activation_token: nil, email_confirmed_at: "2014-09-30 15:20:38", role: 4>} missing required keys: [:id]
Pointing to the following code in the view:
<%= link_to(t('views.shared.navbar.edit_user_profile'), edit_user_path(current_user), class: 'btn btn-primary') %>
If I remove the scope from the routing file things work but the selected locale is not displayed in the url.
Can anyone help?

Everything is fine except passing a :locale param while path generation. Look at the exception, there's: :locale=>#<User.... Since params[:locale] is nil, it takes user as first arg. Extend your app controller with:
def default_url_options(options={})
{ locale: I18n.locale }
end

Related

Ruby on Rails (Redmine) add route

Please help me add the custom route in format:
/projects/any-project-name/estimated_time/report
controller: estimatedtime
method: report
routes.rb
Rails.application.routes.draw do
root :to => 'welcome#index', :as => 'home'
.....
get '/projects/:project_id/issues/gantt', :to => 'gantts#show', :as => 'project_gantt'
get '/issues/gantt', :to => 'gantts#show'
.....
resources :time_entries, :controller => 'timelog', :except => [:show, :edit, :update, :destroy] do
get 'report', :on => :collection
end
.....
resources :time_entries, :controller => 'timelog', :only => [:new, :create]
.....
end
Try this out: get 'projects/:project_name_attribute/estimated_time/report' => 'estimatedtimes#report'

Routes for create action

I have my user_controller and the action create. I do however want to set a custom route to the create action so when the user hits register the create action sets the url to user/thank-you. I have added the following match to my routes file but the url stays unchanged after hitting the register button. Here is part of my routes file until the line with thank-you.
get '/:locale' => 'pages#home'
root :to => 'pages#home'
scope "(:locale)", locale: /en|es/ do
get "javascripts/dynamic_cities"
get "javascripts/dynamic_cities_to_stadia"
get "javascripts/dynamic_stadia"
get "javascripts/dynamic_modality_to_max_players"
get "tournaments/edit_info"
get "tournaments/seasons"
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
resources :admin_tasks, :only => [:index]
resources :sports
resources :countries
resources :cities
resources :stadia
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :teams
resources :tournaments
resources :leagues
resources :brackets, :only => [:new, :create, :edit, :update, :show]
resources :labs, :only => [:show, :edit]
resources :seasons, :only => [:edit, :show, :destroy]
resources :matches, :only => [:show, :edit, :update]
resources :player_participations
resources :highlights
resources :users do
resources :requests, :name_prefix => "user_"
end
# match 'league/:id/subscription' => 'league_rosters#new', :as => :subscription
match '/contact', :to => 'pages#contact'
match '/terms_and_conditions', :to => 'pages#terms_and_conditions'
match '/about_us', :to => 'pages#about_us'
match '/cookie_excluder', :to => 'pages#cookie_excluder'
match '/vacancies', :to => 'pages#vacancies'
match '/signup', :to => 'users#new'
match '/thank-you', :to => 'users#create'
Here is also the create method in the users_controller.rb
def create
# Note: This function is repeated in request controller in the invitations part. So any change should be added to request controller aswell
#user = User.new(email: params[:user][:email].downcase,
name: params[:user][:name].capitalize,
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation],
language: params[:user][:language])
#for user_menu
#title = t("user.new.title")
if #user.save
confirmation_code = "#{#user.id}#{random_string}"
if #user.update_attributes(confirmation_code: confirmation_code)
UserMailer.welcome_email(#user).deliver
vars = Hash.new
vars[:cc] = "#{confirmation_code}"
confirmation_url = url_maker(params[:request_protocol], params[:request_host_with_port], params[:request_locale], "#{email_confirmation_path}", vars)
UserMailer.confirm_email(#user, confirmation_url).deliver
sign_in #user
#user_tournaments = current_user.tournaments.all(:order => "name ASC")
#user_teams = current_user.teams.all(:order => "name ASC")
else
render 'new'
end
else
render 'new'
end
end
Is it possible for this to work the way I am approaching it or I will have to make a redirect and a thank-you page, which I can easily manage through my pages controller?
Move your thank-you route above the resources block as follows:
...
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
# Move route here
match '/thank-you', :to => 'users#create', via: [:post]
resources :admin_tasks, :only => [:index]
resources :sports
...
I'm also adding via: [:post] to the route since:
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
[http://guides.rubyonrails.org/routing.html]

Rails 3.1 Devise all of a sudden redirecting to wrong path

My login was working fine but I don't know where I messed things up. When I login it redirects me sessions/user which is wrong. Here is the error:
No route matches [POST] "/sessions/user"
Here is my routes.rb:
Wal::Application.routes.draw do
resources :sessions, :only => [:new, :create, :destroy]
devise_for :users, :skip => [:sessions]
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :posts do
resources :comments
end
resources :users, :only => [:show]
resources :microsposts, :only => [:create, :destroy]
match '/' => "home#index", :as => :home
match 'home/index', :to => 'home#index'
get "users/show"
root :to => "home#index"
#get "home/index"
resources :users do
member do
get :following, :followers
end
end
resources :microsposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
current_user # <- Path you want to redirect the user to after signup
end
def after_sign_up_path_for(resource)
current_user
end
Try something like this
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
stored_location_for(resource) || welcome_path
end
end
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in-out
Don't know if this will solve your problem, but the first line:
Wal::Application.routes.draw do
has no corresponding 'end' in the code that you posted.

Rails 3.0 member routing problem

I have a resource defined in my routes file as follows:
resources :accounts, :only => [:show, :new, :edit, :create, :update], :member => {
:profile_avatar => :get
}
In turn, in my accounts#show view I have the following code:
<%= image_tag(profile_avatar_account_path(#account, :jpg), :alt => "#{#account.username}", :title => "#{#account.username}") %>
When pulling up the page I get the following error in my production log:
ActionView::Template::Error (undefined method `profile_avatar_account_path' for #<#<Class:0x7f3fdb166260>:0x7f3fdb7bc4e8>):
Does rails 3.0 not support member anymore or is there a different way of doing this?
Thank you,
Brian
It should be
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get 'profile_avatar'
end
# or
get 'profile_avatar', :on => :member
end
Try:
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
get => 'profile_avatar', :on => :member
end
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get :profile_avatar
end
end

How to avoid resolving a route in rails?

I'm developing a rails app with an admin section and a public section. In the public section I want to resolve only index and show routes
resources :services, :only => [:index, :show]
However, when I hit the standard URL for the new action it resolves to the show action with an id of 'new'. That is http://foo.com/services/new returns an error "Couldn't find Service with ID=new". Is there anyway I can tell rails NOT to resolve /services/new?
I've already tried
resources :services, :only => [:index, :show], :except => :new
and
resources :services, :except => :new, :only => [:index, :show]
without success.
ETA (by request):
My entire routes file (sans comments):
MyApp::Application.routes.draw do
resources :services, :only => [:index, :show]
resources :packages,:only => [:index, :show]
get "pages/home"
get "pages/about"
get "pages/help"
root :to => 'packages#index'
namespace "admin" do
get "pages/home"
get "pages/about"
get "pages/help"
resources :services
match "/services/:id/add_to_package/:package_id" => "services#add_package", :as => :add_package_to_service, :via => :post, :id => /\d+/, :package_id => /\d+/
match "/services/:id/remove_from_package/:package_id" => "services#remove_package", :as => :remove_package_from_service, :via => :post, :id => /\d+/, :package_id => /\d+/
resources :packages
match "/packages/:id/add_service/:service_id" => "packages#add_service", :as => :add_service_to_package, :via => :post, :id => /\d+/, :service_id => /\d+/
match "/packages/:id/remove_service/:service_id" => "packages#remove_service", :as => :remove_service_from_package, :via => :post, :id => /\d+/, :service_id => /\d+/
resources :users
root :to => 'pages#home'
end
end
You can try to putting a constraint on your :id param
resources :services, :constraints => {:id => /\d+/}, :only => [:index, :show]
This is assuming your :ids are all number based.
I had a similar situation with a redirect vs resource collision, this fixed it.

Resources