Normally its like:
resources :users
To keep a value ahead we can do like
scope :url do
resources :users
end
but, issue is that I have the just variable in front of the routes not a fixed value with few conditions for it.
Example in rails 2. and want to convert the routing accoding to rails5
map.with_options :controller => 'users' do |user|
user.forgot_user ':url/users/forgot', :action => 'forgot', :url => /([a-zA-Z0-9\-]*)/
user.user ':url/users/retrieve', :action => 'retrieve', :url => /([a-zA-Z0-9\-]*)/
user.login ':url/users/login', :action => 'login', :url => /([a-zA-Z0-9\-]*)/
user.logout ':url/users/logout', :action => 'logout', :url => /([a-zA-Z0-9\-]*)/
user.new_user ':url/users/new', :action => 'new', :conditions => { :method => :get }, :url => /([a-zA-Z0-9\-]*)/
user.users ':url/users/:id', :action => 'show', :conditions => { :method => :get }, :url => /([a-zA-Z0-9\-]*)/
end
Thanks in advance.
scope "/(:subdomain)", :defaults => {:subdomain => "default"} do
...
end
use subdomain as a variable.
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.
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.
Now,I have:
map.resources :questions, :collection => {
:query_by_student => :get
}
that matches:
query_by_student_questions GET /questions/query_by_student(.:format)
{:controller=>"questions", :action=>"query_by_student"}
I want add a "POST" request,to the same url:/questions/query_by_student,but the action should be "post_by_student"
waht shall I do?
怎么在Rails的REST基础上对自己定义的url,实现不同http方法对应不同action?
我现在有这:
map.resources :questions, :collection => {
:query_by_student => :get
}
对应路由信息:
query_by_student_questions GET /questions/query_by_student(.:format)
{:controller=>"questions", :action=>"query_by_student"}
我想加一个post请求,同样到/questions/query_by_student,但是action为post_by_student
怎么写?
I don't have a testing app but with rails 2.2, you can try
map.resources :questions, :collection => {
:query_by_student => [:get, :post]
}
and see what it looks like.
Otherwise, you can probably do it manually with something like
map.connect '/questions/query_by_student', :conditions => { :method => :get }, :controller => 'questions', :action => 'query_by_student'
map.connect '/questions/query_by_student', :conditions => { :method => :post }, :controller => 'questions', :action => 'post_by_student'
Here are my Rails 2 routes:
map.with_options :controller => 'foo', :conditions => { :method => :post } do |foo|
foo.one 'one', :action => 'one'
foo.two 'two', :action => 'two'
foo.with_options :special_flag => 'true', :path_prefix => 'special_prefix',
:conditions => { :method => :get } do |bar|
bar.three '', :action => 'for_blank'
bar.four 'another', :action => 'for_another'
end
end
How do I convert this sort of thing to Rails 3? Just keep using with_options in the same way? It becomes wordier in some cases because instead of doing
match '' => 'foo#for_blank'
I'm doing
match '', :action => 'for_blank'
Yeah, with_options still works in Rails 3. Try this out:
map.with_options :controller => 'foo', :via => :post do
match 'one', :action => 'one' #automatically generates one_* helpers
match 'two', :action => 'two' #automatically generates two_* helpers
foo.with_options :special_flag => 'true', :path => 'special_prefix', :via => :get do
match '', :action => 'for_blank'
match 'another', :action => 'for_another', :as => "four" # as will change the helper methods names
end
end
The :via option replaces your ugly conditions hash with a much nicer syntax.
Like this:
#JSON API
defaults :format => 'json' do
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 :users, :sessions
end
Try to stick to the methods the routes provide. They are very powerful in Rails 3 and should provide everything you need. See http://guides.rubyonrails.org/routing.html for more details