How do I set the path in my rails application? - ruby-on-rails

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.

Related

Ruby on Rails: Linking issue between pages

I'm fairly new to Ruby and Rails and this has been an issue I can't resolve on my own:
I'm working on an existing website, running on an older version of Rails and Ruby 1.9.3.
I'm trying to change the details for a certain user, but sometimes in the linking I don't get http://127.0.0.1:3000/users/firstname.lastname but http://127.0.0.1:3000/users?id=firstname.lastname.
This is the code for the links I have:
<li><%= link_to "Remove this user", user_path(#person[:uid]), :confirm => 'Are you sure you want to delete ' + #person[:uid] + '?',:method => :delete ,:title => "Delete user from LDAP" %></li>
<li><%= link_to "Edit this user", edit_userdetails_path(id:#username[:UserID]) %></li>
<li><%= link_to "Add to group", group_path %></li>
<li><%= link_to "Change password", change_pwd_path(#person[:uid]) %></li>
The first and 3rd link work like they should, the 2nd and 4th link have the behavior as described with the ?id= in the url.
This is what's in my routes file:
resources :posts
resources :timesheets
resources :personal_bugs, :only => [:new, :create, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :passwords, :only => [:new, :create]
resources :users, :constraints => { :id => /[0-9A-Za-z\-\.]+/ }
resources :computers
resources :userdetails, :constraints => { :id => /[0-9A-Za-z\-\.]+/ }
resources :bz2refs, :only => [:index, :update]
resources :bz2ref_activities
resources :sessions do
collection do
get :reset_password
get :recovery
end
end
resources :bz2ref_resources do
collection do
get :remove
get :restore
end
end
get "userdetails" => "userdetails#index", :as => "userdetails"
get "edit_userdetails" => "userdetails#edit", :as => "edit_userdetails"
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "change_pwd" => "passwords#new", :as => "change_pwd"
post "search" => "pages#search", :as => "search"
get "search" => "pages#search", :as => "search"
get "help" => "pages#help", :as => "help"
get "admin_urls" => "pages#admin_urls", :as => "admin_urls"
get "report" => "timesheets#report", :as => "report"
post "report" => "timesheets#report", :as => "report"
get "incomplete" => "timesheets#incomplete", :as => "incomplete"
post "incomplete" => "timesheets#incomplete", :as => "incomplete"
get "check" => "timesheets#check", :as => "check"
post "check" => "timesheets#check", :as => "check"
get "export" => "timesheets#export", :as => "export"
get "group" => "users#group", :as => "group"
get "group_show" => "users#group_show", :as => "group_show"
post "group_add" => "users#group_add", :as => "group_add"
get "computer_wake" => "computers#wake", :as => "computer_wake"
get "mirror_users" => "users#mirror", :as => "mirror_users"
get "kiosk" => "posts#latest", :as=> "kiosk"
get "lookupbug" => "timesheets#lookupbug", :as =>"lookupbug"
I call upon userdetails controller from users and then after editing I want to go back from userdetails --> users.
That is because you give an id parameter in the path.
So for example:
user_path(#person[:uid]) => localhost/users/firstname.lastname
user_path(id: #person[:uid]) => localhost/users?id=firstname.lastname
To fix the second link you need to change from edit_userdetails_path(id:#username[:UserID]) to edit_userdetails_path(#username[:UserID]), without the id.
I solved the issue by creating the hyperlink this way:
<li><%= link_to "Edit this user", controller: "userdetails", action: "edit", id: #username[:UserID] %></li>
This way it works!

Rails Devise disable or remove user registration but keep user edit path

I want to disable user registration path and keep user edit path. I used this guide for devise Solution 2.
But I am getting this error
ActionController::UnknownFormat at /users.55840776527573104d0c0000
ActionController::UnknownFormat
The form generates incorrect url, it should be like that
/users/55840776527573104d0c0000
View
<%= form_for(resource, as: resource_name, url: user_registration_path(resource), html: { method: :patch}) do |f| %>
<%= f.submit "Update"%>
routes.rb
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
patch'users' => 'devise/registrations#update', :as => 'user_registration'
end
How to fix it?
Solution for this problem is to add :id into routes.rb - patch
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
patch'users/:id' => 'devise/registrations#update', :as => 'user_registration'
end
Found solution here

What does ActionController::Routing::Routes.draw do |map| do?

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.

Refactoring Rails 3 Routes

I have this in my routes:
get '/boutique/new' => 'stores#new', :as => :new_store, :constraints => { :id => /[a-z0-9_-]/ }
post '/boutique' => 'stores#create', :as => :create_store, :constraints => { :id => /[a-z0-9_-]/ }
get '/:shortname' => 'stores#show', :as => :store, :constraints => { :id => /[a-z0-9_-]/ }
get '/:shortname/edit' => 'stores#edit', :as => :edit_store, :constraints => { :id => /[a-z0-9_-]/ }
put '/:shortname' => 'stores#update', :as => :update_store, :constraints => { :id => /[a-z0-9_-]/ }
delete '/:shortname' => 'stores#delete', :as => :destroy_store, :constraints => { :id => /[a-z0-9_-]/ }
Is there a cleaner way to do the same? It doesn't look any elegant and even less if I add some more controls/actions to it.
Thank you.
Your best option would be to stick to the standard resource routes. If anyone else ever needs to work on the application you're building, they'll thank you.
That said, if you really need this routing setup (for whatever reason), try the following:
controller :stores do
constraints :id => /[a-z0-9_-]/ do
get '/boutique/new' => :new, :as => :new_store
post '/boutique' => :create, :as => :create_store
get '/:shortname' => :show, :as => :store
get '/:shortname/edit' => :edit, :as => :edit_store
put '/:shortname' => :update, :as => :update_store
delete '/:shortname' => :delete, :as => :destroy_store
end
end
I haven't actually tested it, but that should work fine.

Is there a better way to do with_options in Rails 3 routes?

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

Resources