Ruby on Rails (Redmine) add route - ruby-on-rails

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'

Related

Routing in Rails fails when changing application to Multilingual Application

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

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]

add new routes to a resource without the added param?

I would like to add a url to my comments route so I can call "post_comments_latest_path". I added something like 'get "comments/latest" => "comments#latest", :as => "latest"' but the route adds and the :commend_id to the path that is not needed. Any suggestions?
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_reply"
get "replies/new" => "comments#new_reply", :as => "new_reply"
end
end
This should work:
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_rely"
get "replies/new" => "comments#new_reply", :as => "new_reply"
get "latest", :on => "collection"
end
end
A Member route is one that links to a specific resource; requires an id.
A Collection route is one that links to a resource collection; does not require an id.
See the Rails Routing Guide for more information.

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.

Rails 2.3.8 routing, how do I create a route without a controller name on nested resources?

Well I have the following code for routing with nested resources:
map.resources :cities, :as => "cidade", :only => [:index, :show] do |city|
city.resources :offers, :as => "oferta", :only => [:show], :collection => [:recents], :member => [:share, :buy, :add, :add_gift, :remove, :validate_buy, :want_more, :withdraw_credits], :path_names => { :want_more => "quero-mais", :recents => "recentes", :buy => "comprar", :add_gift => "comprar-presente", :share => "compartilhar", :add => "adicionar", :remove => "remover", :validate_buy => "validar-compra", :withdraw_credits => "resgatar-creditos" } do |offer|
offer.resources :photos, :as => "fotos", :only => [:index]
offer.resources :videos, :as => "videos", :only => [:index, :show]
offer.resources :comments, :as => "comentarios", :only => [:index, :new, :create]
end
end
The thing is I don't want all those ':as =>' to be on the url, by this I mean that I don't want the controllers names on the url, istead of generating /cidades/curitiba/ofertas/1 I only want /curitiba/1.
I've tried :path_prefix and :as => "", but those did not work.
any help?
thanks
What if you try this after the map.resources definition in routes.rb?
match '/:city_name/:id', :to => "offers#show"
Then you do whatever you want in offers_controller#show

Resources