What does ActionController::Routing::Routes.draw do |map| do? - ruby-on-rails

I have the following code in a routes file which I haven't come across before, but need to get my head around it but I don't have a clue what's going on because I've only really used match ...
ActionController::Routing::Routes.draw do |map|
map.devise_for :users, :controllers => { :confirmations => 'confirmations' }
map.resources :sections, :only => [:index, :show]
def stories(parent)
parent.resources :stories, :only => [:index, :show], :collection => {:search => :get} do |story|
story.resources :videos, :only => [:index], :member => {:play => :get, :start_download => :get}
story.resources :images, :only => [:index], :member => {:start_download => :get}
story.resources :documents, :only => [:index], :member => {:start_download => :get}
story.resources :audios, :only => [:index], :member => {:play => :get, :start_download => :get}
end
end
stories(map)
map.namespace :manage do |client_admin|
client_admin.resources :stories do |story|
story.resources :meta_datas
story.resources :media_files, :collection => { :clone_metadata => :get }, :member => { :crop_thumbnail => :get } do |mf|
mf.resources :media_meta_datas
end
end
client_admin.resources :users, :only => [:new, :create, :index]
end
map.ftp_upload 'manage/stories/:story_id/ftp', :controller => 'manage/media_files', :action => 'ftp'
map.page ':page', :controller => 'pages', :action => 'show', :page => /terms/
map.contact '/contact', :controller => 'contact', :action => 'index'
map.resource :password_reminder
map.library_redirect '/library', :controller => 'media_files', :action => 'redirect'
map.library '/library/:media_type', :controller => 'media_files', :action => 'index'
map.press_kits '/corporate-information', :controller => 'stories', :action => 'press_kits'
map.resource :global_toolkit, :controller => 'global_toolkit', :as => 'global-toolkit', :only => 'show' do |toolkit|
toolkit.library_redirect '/library', :controller => 'global_toolkit_media_files', :action => 'redirect'
toolkit.library '/library/:media_type', :controller => 'global_toolkit_media_files', :action => 'index'
toolkit.press_materials '/press-materials', :controller => 'stories', :action => 'press_materials'
toolkit.brand_guidelines '/brand-guidelines', :controller => 'stories', :action => 'brand_guidelines'
toolkit.martini_contacts '/martini-contacts', :controller => 'stories', :action => 'martini_contacts'
stories(toolkit)
end
map.root :sections
end
When I go to the URL /global-toolkit/brand-guidelines1 it says unknown action but as far as I can figure out it should work.

Related

Rails route to specific action for resource

I'm trying to get the url for given params:
url_for({
:action => "show",
:controller => "questions",
:dashboard_id => "123",
:dashboard_type => "mono",
:question_id => "1234",
:only_path => true
})
But I get this error:
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"questions", :dashboard_id=>"123", :dashboard_type=>"mono", :question_id=>"1234"}
In my routes.rb file, I have this configuration:
resources :dashboards, :only => [:index, :all, :create] do
resources :questions, :path => '/:dashboard_type/questions'
end
What seems to be the problem?
Replace :question_id with :id
It should be
url_for({
:action => "show",
:controller => "questions",
:dashboard_id => "123",
:dashboard_type => "mono",
:id => "1234",
:only_path => true
})
As there is no :question_id
dashboard_question GET /dashboards/:dashboard_id/:dashboard_type/questions/:id(.:format) questions#show

How do I set the path in my rails application?

How do I set the path?
I get the following error when I run my rails application:
No route matches {:action=>"search", :controller=>"inbox"} missing required keys: [:id]
I think this has to do with my routes.rb file.
I am working on a search bar and every time I search, it loops back to the main page since I do not have a path set.
This is my index.erb.html file.
<%= form_tag inbox_search_path, :method => 'get' do %>
<p>
<%= search_field_tag :Search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
And this is my routes.rb file.
Rails.application.routes.draw do
resources :webinars
resources :topic_proposals
resources :webinar_questions
get 'webinars/show/' => 'webinars#show', :as => 'webinars_show'
get 'webinars/edit/:id' => 'webinars#edit', :as => 'webinars_edit'
get 'webinars/delete_webinar/:id' => 'webinars#delete_webinar', :as => 'webinars_delete_webinar'
resources :categories_skills
resources :categories
get 'home/download_pdf'
get 'static/:view' => 'static#show', :as => 'static'
get 'lists/:id/members' => 'lists#members', :as => 'list_members'
post 'lists/:id/update_mailchimp_list' => 'lists#update_mailchimp', :as => 'update_mailchimp_list'
# put 'lists/:id/' => 'lists#update', :as => 'list_update'
# get 'lists' => 'lists#index', :as => 'lists'
resources :lists
resources :emails
post 'emails/upload' => 'emails#upload', :as => 'upload_emails'
resources :campaigns
resources :affiliates
resources :answers
resources :questions
resources :messages
resources :schools
resources :payments
resources :assessments do
resources :questions do
resources :responses
end
end
get 'tryout' => "tryout#index"
get 'take/assessments' => 'take_assessment#show', :as => 'take_assessments'
get 'take/assessments/start' => 'take_assessment#start', :as => 'start_assessments'
get 'take/assessments/summary' => 'take_assessment#summary', :as => 'summary_assessment'
get 'take/assessments/select_school_year' => 'take_assessment#select_school_year', :as => 'select_school_year'
get 'quiz-results' => 'quiz_results#summary', :as => 'quiz-results'
match 'connect/mentors' => 'connect_mentors#index', :as => 'connect_mentors', via: [:get, :post]
get 'connect/mentors/:id' => 'connect_mentors#show', :as => 'show_mentor'
get 'connect/students/:id' => 'connect_students#show', :as => 'show_student'
devise_scope :user do
get 'partners/:invitation_code' => 'partners#landing', as: 'partners_landing'
get 'partners/:code/new' => 'partners#new', as: 'partners_new'
post 'partners' => 'partners#create', as: 'partners_registration'
end
devise_scope :user do
get 'members/new' => 'members#new', as: 'members_new'
get 'members/new/:variation' => 'members#new', as: 'members_new_variation'
post 'members' => 'members#create', as: 'members_registration'
end
resource :user, only: [:edit] do
collection do
patch 'update_password'
patch 'update_notification_email'
patch 'update_school_year_id', :controller=>"take_assessment"
end
end
get 'learn' => "lesson_plans#index"
get 'lesson-plans/:id' => 'lesson_plans#show', as: 'lesson_plan'
get 'content/:id' => 'content#show', as: :content
get 'content/:id/review' => 'content#review', as: 'content_review'
get 'mentors/:id/conversation' => 'conversations#index', as: 'mentor_conversation'
get 'mentors/:id/conversations/new' => 'conversations#new', as: 'new_mentor_conversation'
get 'profile' => 'profile#show', as: 'user_profile'
get 'redeem' => 'redeem_invitations#validate_redeem_code', as: 'redeem_code'
resources :request_invitations
resources :payment_types
resources :articles
resources :skills
resources :portals
resources :school_sessions
resources :school_years
resources :conversations do
resource :messages, only: [:new, :create]
end
resources :grade_levels
resources :messages, only: [:index, :show]
resources :lookup do
get :lookup_mentor_name, :on => :collection
get :lookup_student_name, :on => :collection
end
get 'inbox' => 'inbox#index', :as => 'inbox_index'
get 'inbox/show/:id' => 'inbox#show', :as => 'inbox_show'
get 'inbox/compose' => 'inbox#compose', :as => 'inbox_compose'
get 'inbox/compose/:id' => 'inbox#compose', :as => 'inbox_compose_to'
get 'inbox/edit/:id' => 'inbox#edit', :as => 'inbox_edit'
get 'inbox/move' => 'inbox#move', :as => 'inbox_move'
get 'inbox/reply' => 'inbox#reply', :as => 'inbox_reply'
post 'inbox/create' => 'inbox#create'
post 'inbox/bulk' => 'inbox#bulk'
post 'memberships/agree' => 'memberships#agree'
get 'memberships/new' => 'memberships#new', :as => 'memberships_new'
get 'memberships/terms' => 'memberships#terms', :as => 'memberships_terms'
get 'memberships/success/:id' => 'memberships#success', :as => 'memberships_success'
get 'memberships/failure' => 'memberships#failure', :as => 'memberships_failure'
get 'memberships/paypal_payment' => 'memberships#paypal_payment', :as => 'paypal_payment'
get 'memberships/paypal_payment_step_2' => 'memberships#paypal_payment_step_2', :as => 'paypal_payment_step_2'
resources :memberships
post 'memberships/hook' => 'memberships#hook'
get 'admin/dashboard/export_cert' => 'admin_dashboard#export_certs', :as => 'admin_dashboard_export_cert'
get 'admin/dashboard/registrations' => 'admin_dashboard#registrations', :as => 'admin_dashboard_registrations'
get 'admin/dashboard/payments' => 'admin_dashboard#payments', :as => 'admin_dashboard_payments'
get 'admin/dashboard/schoolreports' => 'admin_dashboard#schoolreports', :as => 'admin_dashboard_schoolreports'
get 'admin/dashboard/memberreports' => 'admin_dashboard#memberreports', :as => 'admin_dashboard_memberreports'
get 'admin/dashboard/messagingreports' => 'admin_dashboard#messagingreports', :as => 'admin_dashboard_messagingreports'
get 'admin/dashboard/messaging_mentor_reports' => 'admin_dashboard#messaging_mentor_reports', :as => 'admin_dashboard_messaging_mentor_reports'
get 'admin/dashboard/load_points' => 'admin_dashboard#load_points', :as => 'admin_dashboard_load_points'
get 'admin/dashboard/reset_points' => 'admin_dashboard#reset_points', :as => 'admin_dashboard_reset_points'
get 'admin/dashboard/activate_membership' => 'admin_dashboard#activate_membership', :as => 'admin_dashboard_activate_membership'
get 'admin/dashboard/delete_account' => 'admin_dashboard#delete_account', :as => 'admin_dashboard_delete_account'
get 'admin/dashboard/reset_password/:id' => 'admin_dashboard#reset_password', :as => 'admin_dashboard_reset_password'
get 'admin/members/edit/:id' => 'admin_dashboard#edit', :as => 'edituser'
get 'admin/dashboard/unpaid_members' => 'admin_dashboard#unpaid_members', :as => 'unpaid_members'
patch 'admin/members/edit/:id' => 'admin_dashboard#update' # new code
get 'admin/dashboard/expired_messages' => 'admin_dashboard#expired_messages', :as => 'admin_dashboard_expired_messages'
get 'admin/dashboard/' => 'admin_dashboard#search', :as => 'admin_dashboard'
get 'admin/demo_list/' => 'admin_dashboard#demo_list', :as => 'demo_list'
get 'admin/cleandata/:id' => 'admin_dashboard#data_cleanup', :as => 'data_cleanup'
post 'admin/dashboard/upload' => 'admin_dashboard#upload', :as => 'upload_admin_dashboard'
get 'admin/mentors' => 'mentors#index', :as => 'admin_mentors'
post 'admin/mentors' => 'mentors#create'
get 'admin/mentors/new' => 'mentors#new', :as => 'admin_new_mentor'
get 'admin/mentors/:id' => 'mentors#show', :as => 'admin_mentor'
get 'admin/impersonate'=> 'admin_dashboard#impersonate', :as => 'admin_impersonate'
get 'admin/stop_impersonating'=> 'admin_dashboard#stop_impersonating', :as => 'admin_stop_impersonating'
get 'messages/reply/:id' => 'messages#reply', as: 'reply_to'
get 'credits/new', as: 'new_credits'
get 'dashboard' => 'dashboard#index', as: 'user_root'
get 'seen_message' => 'dashboard#seen_message', as: 'seen_message'
devise_for :users, :controllers => {registrations: 'registrations', passwords: 'passwords'}
# The static site pages
get 'about' => "pages#about", :as => 'about'
get 'joinus' => "pages#membership", :as => 'joinus'
get 'faq' => "pages#faq", :as => "faq"
get 'certification' => "pages#certification", :as => "certification"
get 'chapters' => "pages#chapters", :as => "chapters"
get 'internships' => 'pages#internships', :as => 'internships'
get 'privacy-policy' => 'pages#privacy_policy', :as => 'privacy_policy'
get 'contact-us' => 'pages#contact_us', :as => 'contact_us'
post 'send_message' => "pages#send_message", :as => 'send_contact_us'
get 'terms-of-use' => 'pages#terms', :as => 'terms'
#temporary
get 'pages/accountinfo' => 'pages#accountinfo', :as => "pages_accountinfo"
get 'pages/registration' => 'pages#registration', :as => "pages_registration"
get 'pages/payment' => 'pages#payment', :as => "pages_payment"
# leadership pages
get 'leadership' => 'leadership#index'
get 'leadership/recruitment' => 'leadership#recruitment', :as => "leadership_recruitment"
get 'leadership/advisory' => 'leadership#advisory', :as => "leadership_advisory"
get 'leadership/fredsiegel' => 'leadership#fredsiegel', :as => :fredsiegel
get 'leadership/matttrainum' => 'leadership#matttrainum', :as => :matttrainum
get 'leadership/lenormanstrong' => 'leadership#lenormanstrong', :as => :lenormanstrong
get 'leadership/arlenecash' => 'leadership#arlenecash', :as => :arlenecash
get 'leadership/ernestolopez' => 'leadership#ernestolopez', :as => :ernestolopez
get 'leadership/rodneyjohnson' => 'leadership#rodneyjohnson', :as => :rodneyjohnson
get 'leadership/carolaweil' => 'leadership#carolaweil', :as => :carolaweil
get 'leadership/dianenafranowicz' => 'leadership#dianenafranowicz', :as => :dianenafranowicz
get 'leadership/alexakim' => 'leadership#alexakim', :as => :alexakim
get 'leadership/robertkirkwood' => 'leadership#robertkirkwood', :as => :robertkirkwood
get 'leadership/jodyhubbard' => 'leadership#jodyhubbard', :as => :jodyhubbard
get 'leadership/bobbydishell' => 'leadership#bobbydishell', :as => :bobbydishell
get 'leadership/paulhimmelfarb' => 'leadership#paulhimmelfarb', :as => :paulhimmelfarb
get 'leadership/nedjohnson' => 'leadership#nedjohnson', :as => :nedjohnson
get 'leadership/patsidhu' => 'leadership#patsidhu', :as => :patsidhu
get 'leadership/product' => 'pages#product', :as => :product
# internship pages
resources :internship_application
get 'internship' => 'internship_application#index', :as => 'internship'
# Payments Search
get 'internship' => 'payments#search', :as => :paymentssearch
# feature pages
get 'feature/mentor' => 'feature#mentor', :as => "feature_mentor"
get 'feature/advisor' => 'feature#advisor', :as => "feature_advisor"
get 'memberships/accountinfo' => 'memberships#accountinfo', :as => "membership_accountinfo"
resources :contacts, only:[:new, :create]
root 'home#index'
# We need this line to catch any routing error
match '*path(.:format)', :to => 'application#not_found', via: :all
end
This is my relevant part of route.rb file.
get 'inbox' => 'inbox#index', :as => 'inbox_index'
get 'inbox/show/:id' => 'inbox#show', :as => 'inbox_show'
get 'inbox/compose' => 'inbox#compose', :as => 'inbox_compose'
get 'inbox/compose/:id' => 'inbox#compose', :as => 'inbox_compose_to'
get 'inbox/edit/:id' => 'inbox#edit', :as => 'inbox_edit'
get 'inbox/move' => 'inbox#move', :as => 'inbox_move'
get 'inbox/reply' => 'inbox#reply', :as => 'inbox_reply'
post 'inbox/create' => 'inbox#create'
post 'inbox/bulk' => 'inbox#bulk'
get 'inbox/search' => 'inbox#seaarch', :as => 'inbox_search'
Add this route to your routes.rb
get 'inbox/search' => 'inbox#search', :as => inbox_search
it looks like your route has a spelling error which could cause it to fail.
get 'inbox/search' => 'inbox#seaarch', :as => 'inbox_search'
^
So changing your form_tag to this:
<%= form_tag '/inbox/search', :method => 'get' do %>
<p>
<%= search_field_tag :Search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
should fix your path helper problem. as to why you're getting that error it looks like inbox_search_path is expecting you to pass in an id so either you changed your route file, you need to bounce your server, or you need to paste the rest of your html file.

Redmine plugin show multiple controller in same menu tab

I'm creating a Redmine plugin with a menu tab. Follow the guide
http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial
I can add a menu tab in file init.rb as below:
permission :polls, { :polls => [:index, :vote] }, :public => true
menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id
The menu tab is OK for PollsController.
But I create a new controller (for example, ArticlesController) in the same plugin. There's a link in polls#index to go to articles#new. But articles#new don't appear in the menu, it appear as I didn't create menu.
How can I use the 'Polls' menu tab for ArticlesController.
I tried:
class ArticlesController < ApplicationController
menu_item :pivot_table
....
end
and
permission :polls, { :polls => [:index, :vote] }, :public => true
permission :polls, { :articles => [:index, :vote] }, :public => true
menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id
but no luck.
Here my routes.rb:
RedmineApp::Application.routes.draw do
resources :projects do
resources :polls, :only => [:index]
resources :articles, :only => [:new, :create]
end
resources :articles, :except => [:show]
end
Pleae help me. My redmine version is 2.3.2. Thank you.
Not sure if it's what you are asking for, but to get 2 tabs in the project menu, you could try :
Redmine::MenuManager.map :project_menu do |menu|
menu.push :polls, { :controller => 'polls', :action => 'index' }, :param => :project_id, :caption => 'Polls', :after => :activity
menu.push :articles, { :controller => 'articles', :action => 'new' }, :param => :project_id, :caption => 'Articles', :after => :activity
end
I found my problem. It's the permission in init.rb
My error configuration:
permission :polls, { :polls => [:index, :vote] }, :public => true
permission :polls, { :articles => [:index, :vote] }, :public => true
menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id
The name "permission :polls" is duplicated. I'm wrong. I think that ":polls" is name. But it's permission's name. I change to another name, it's OK.
permission :polls, { :polls => [:index, :vote] }, :public => true
permission :articles_permission, { :articles => [:index, :vote] }, :public => true
menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id

Custom dynamic error pages in Ruby on Rails not working

I'm trying to implement custom dynamic error pages following this post:
http://www.perfectline.co.uk/blog/custom-dynamic-error-pages-in-ruby-on-rails
I did exactly what the blog post says. I included config.action_controller.consider_all_requests_local = false in my environment.rb. But is not working.
My browser shows:
Routing Error
No route matches "/555" with {:method=>:get}
So, it looks like the rescues are not fired.
I get the following in my log file:
ActionController::RoutingError (No route matches "/555" with {:method=>:get}):
Rendering rescues/layout (not_found)
Is there some routing interfering with the code? I'm not sure what to look for. I'm running rails 2.3.5.
Here is the routes.rb file:
ActionController::Routing::Routes.draw do |map|
# routing van property-url
map.connect 'buy/:property_type_plural/:province/:city/:address/:house_number', :controller => 'properties' , :action => 'show', :id => 'whatever'
map.myimmonatie 'myimmonatie' , :controller => 'myimmonatie/properties', :action => 'index'
map.login "login", :controller => "user_sessions", :action => "create", :conditions => {:method => :post}
map.login "login", :controller => "user_sessions", :action => "new"
map.logout "logout", :controller => "user_sessions", :action => "destroy"
map.buy "buy", :controller => 'buy'
map.sell "sell", :controller => 'sell'
map.home "home", :controller => 'home'
map.disclaimer "disclaimer", :controller => 'disclaimer'
map.sign_up "sign_up", :controller => 'users', :action => :new
map.contact "contact", :controller => 'contact'
map.resources :user_sessions
map.resources :contact
map.resources :password_resets
map.resources :messages
map.resources :users, :only => [:index,:new,:create,:activate,:edit,:profile,:password]
map.resources :images
map.resources :activation , :only => [:new,:resend]
map.resources :email
map.resources :properties, :except => [:index,:destroy]
map.namespace :admin do |admin|
admin.resources :users
admin.resources :properties
admin.resources :order_items, :as => :orders
admin.resources :blog_posts, :as => :blog
end
map.connect 'myimmonatie/:action' , :controller => 'users', :id => 'current', :requirements => {:action => /(profile)|(password)|(email)/}
map.namespace :myimmonatie do |myimmonatie|
myimmonatie.resources :messages, :controller => 'messages'
myimmonatie.resources :password, :as => "password", :controller => 'users', :action => 'password'
myimmonatie.resources :properties , :controller => 'properties'
myimmonatie.resources :orders , :only => [:index,:show,:create,:new]
end
map.root :controller => "home"
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
ActionController::Routing::Translator.translate_from_file('config','i18n-routes.yml')
The code works, something is wrong with the line in my environment.rb
config.action_controller.consider_all_requests_local = false
It seems to be overridden somewhere.
So, CASE CLOSED, thanks!
Rails detects when you are browsing with ip 127.0.0.1 and shows you the development environment errors even if you are in production environment. You should try accessing from a different machine to get the proper errors.

Refactoring routes - serving different layouts

As a Rails NOOB, I started with a routes.rb of:
ActionController::Routing::Routes.draw do |map|
map.resources :events
map.connect 'affiliates/list', :controller => "affiliates", :action => "list"
map.connect 'affiliates/regenerate_thumb/:id', :controller => "affiliates", :action => "regenerate_thumb"
map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
map.resources :affiliates, :has_many => :events
map.connect ":controller/:action"
map.connect '', :controller => "affiliates"
map.connect ":controller/:action/:id"
map.connect ":controller/:action/:id/:format"
end
and i'm trying to tighten it up. and I've gotten as far as:
ActionController::Routing::Routes.draw do |map|
map.resources :events, :only => "index"
map.resources :affiliates do |affiliates|
affiliates.resources :has_many => :events
affiliates.resources :collection => { :list => :get, :regenerate_thumb => "regenerate_thumb" }
end
# map.connect 'affiliates/regenerate_thumb/:id', :controller => "affiliates", :action => "regenerate_thumb"
map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
map.root :affiliates
end
what is confusing to me is routes vs parameters.. For example, I realized that the only difference between list and index is HOW it is rendered, rather than WHAT is rendered.
Having a different action (as I do now) feels wrong but I can't figure out he right way.
Thanks
You can refactor it like this:
map.resources :events, :only => "index"
map.resources :affiliates, :has_many => :events, :collection => { :list => :get }, :member => { :regenerate_thumb => :get }
map.connect 'affiliates/state/:id.:format', :controller => "affiliates", :action => "find_by_state"
map.connect 'affiliates/getfeed', :controller => "affiliates", :action => "feed"
map.root :affiliates
The title says "serving different layouts". Where's the problem?

Resources