I have a route as:
match 'api/v1/:id/:format/:date/(:type)', :controller => "xpto", :action => "api_xpto"
The goal is to make it as a GET request from the outside. However, the route is not being set as a GET.
How can I make it a GET?
Change match to get:
get 'api/v1/:id/:format/:date/(:type)', :controller => "xpto", :action => "api_xpto"
Or add :via option
match 'api/v1/:id/:format/:date/(:type)', :controller => "xpto", :action => "api_xpto", :via => :get
Related
Currently I have these two routes in my rake routes output:
user_omniauth_authorize
/users/auth/:provider(.:format)
devise/omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
user_omniauth_callback
/users/auth/:action/callback(.:format)
devise/omniauth_callbacks#(?-mix:facebook|twitter)
What file do I have to change to customize these so the route can read:
user_omniauth_authorize
/admin/manage/:slug/auth/:provider(.:format)
devise/omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
user_omniauth_callback
/admin/manage/:slug/auth/:action/callback(.:format)
devise/omniauth_callbacks#(?-mix:facebook|twitter)
Add the following to your routes.rb file:
devise_for :users, :skip => :omniauth_callbacks
devise_scope :user do
match "/admin/manage/:slug/auth/:provider",
:constraints => { :provider => /facebook|twitter/ },
:to => "devise/omniauth_callbacks#passthru",
:as => :user_omniauth_authorize,
:via => [:get, :post]
match "/admin/manage/:slug/auth/:action/callback",
:constraints => { :action => /facebook|twitter/ },
:to => "devise/omniauth_callbacks",
:as => :user_omniauth_callback,
:via => [:get, :post]
end
Now, this is untested. I copied it from Devise's source code. So there are a couple of problems:
You have to add user to the :as alias, so it's not dynamic.
You have to add the auth providers to the constraints, so it's not dynamic.
I'm using: Rails 3.1.3, HAML and Passenger
I'm having a route problem in my staging branch, but in the other branch that was merged into staging I have no problem at all. I've tried comparing my routes files and the form files from these two branches using git diff but the two files are the same in the branches.
A weird thing is the rendered error page. Here's a link to an screenshot
Error message
And also the error from passenger:
Rendered time_entries/_form_fields.haml (173.2ms)
Rendered time_entries/_form.haml (194.7ms)
Rendered time_entries/edit.haml within layouts/application (196.6ms)
Completed 500 Internal Server Error in 448ms
ActionView::Template::Error (No route matches {:controller=>"time_entries"}):
5: .field
6: = form.submit "Save"
7: |
8: = link_to "Cancel", project_time_entries_path(#project)
app/views/time_entries/_form.haml:8:in `block in _app_views_time_entries__form_haml__894171410_105032920'
app/views/time_entries/_form.haml:3:in `_app_views_time_entries__form_haml__894171410_105032920'
app/views/time_entries/edit.haml:2:in `_app_views_time_entries_edit_haml__805073897_105087770'
I attach you the code from my route and my form
form.haml
.app-form
= error_messages_for :time_entry
= form_for [#project, #time_entry] do |form|
= render :partial => 'form_fields', :locals => {:form => form}
.field
= form.submit "Save"
|
= link_to "Cancel", project_time_entries_path(#project)
routes.rb
Titi::Application.routes.draw do
root :to => 'time_entries#index'
match 'home' => 'home#index', :as => :home
match '/api/*other' => TitiAPI
resources :projects do
member do
get 'archive'
get 'unarchive'
end
resources :viewers
resources :labels
resources :time_entries do
collection do
get 'start'
end
member do
get 'new_note'
put 'add_note'
end
end
end
resources :bookings
resources :technical_orientations, except: [:delete]
match 'merge_labels/:projects_id' => 'labels#merge_labels', :as => :merge_labels
match 'move_labels/:projects_id' => 'labels#move_labels', :as => :move_labels
resources :time_entries do
collection do
get 'start'
end
member do
get 'new_note'
put 'add_note'
end
end
resources :users do
member do
get 'time_entries'
get 'edit_password'
put 'update_password'
get 'suspend'
get 'unsuspend'
end
new do
get 'invite_form'
post 'invite'
end
collection do
get 'all'
end
end
resources :user_sessions
match 'reports/timesheet' => 'reports#timesheet', :as => :timesheet
match 'reports/:week/timesheet' => 'reports#timesheet', :as => :timesheet_week
match 'reports/timesheet_detail' => 'reports#timesheet_detail', :as => :timesheet_detail
match 'reports/dashboard' => 'reports#dashboard', :as => :dashboard_report
match 'reports/timeentries_irregulars' => 'time_entries_irregulars#timeentries_irregulars', :as => :timeentries_irregulars
match 'reports/:week/timeirregulars' => 'time_entries_irregulars#timeentries_irregulars', :as => :timeirregulars_week
match 'reports/timesheet/admin' => 'reports#timesheet_admin', :as => :timesheet_admin
match 'reports/:week/timesheet/admin' => 'reports#timesheet_admin', :as => :timesheet_admin_week
match 'reports/time_entries' => 'time_entries_reports#landing_report'
match 'reports/time_entries/make' => 'time_entries_reports#index'
match 'reports/time_entries/csv' => 'time_entries_reports#make_csv'
match 'reports/:token' => 'reports#index', :as => :reports
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#register', :as => :register
resources :companies do
collection do
get 'settings'
get "remove_photo"
end
end
match 'labels/for_project_id/:id' => 'labels#for_project_id', :as => :for_project_id
match 'holidays_for_company/:token' => 'holidays#public', :as => :public_holiday_for_company
match 'holidays_by_year' => 'holidays#by_year', :as => :holidays_by_year
resources :holidays
resources :clients
resources :invoices do
member do
get 'pdf_invoice'
get 'cancel'
end
resources :invoice_lines do
collection do
get 'add'
end
end
resources :payments
end
resources :tweets do
resource :tweet_likes, :path => 'likes' do
collection do
get 'names'
end
end
end
match 'select_invoices' => 'invoices#select_invoices', :as => :select_invoices
match 'pusher/auth' => 'pusher#auth'
resources :requests, :only=>[:create]
match '*request', :controller => 'requests', :action => 'options', :constraints => {:method => 'OPTIONS', :format => 'json'}
end
Thank's in advance.
On app/views/time_entries/_form.haml:8, does #project exist? Perhaps your staging branch uses a different database and hence the #project you think is there is not?
Also, run rake routes and verify the project_time_entries is in the output. Perhaps you need a :as on one of your routes.
I have an issue where I have an admin subdomain setup in the routes like so:
constraints :subdomain => 'admin' do
scope :module => "admin" do
match 'articles/:id/', :to => 'articles#show'
resources :articles, :events do
collection do
post :update_attribute_on_the_spot
end
end
root :to => "dashboard#index"
end
end
Then after that on articles for the main site I have:
resources :articles, :events, :george
match '/:year/:month/:day/:slug', :to => 'articles#show', :as => "article", :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
match '/event/:year/:month/:day/:slug', :to => 'events#show', :as => "event", :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
I am wondering how I make sure the main site routes are not used when the admin subdomain routes are in effect, as of now when going to the admin section articles show is mapped to the main site route and therefore the admin routes will not function unless that route is removed.
If anyone can show me the best way around this issue it would be great.
Thanks!
I did not realize declaring something with :as would overwrite any other route so simply:
match '/:year/:month/:day/:slug', :to => 'articles#show', :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
Fixed everything!
I have a form that is suppose to be a POST to CREATE action but it is doing a POST to INDEX action. So i then decided to test my routes with rspec. In my Example i have my test as follows.
it "should recognize a specific invoices#create route" do
assert_routing("/invoices", {:controller => "invoices", :action => "create"})
end
but when i run the test its coming up with this error.
1) InvoicesController on get to :index should recognize a specific invoices#create route
Failure/Error: assert_routing("/invoices", {:controller => "invoices", :action => "create"})
The recognized options <{"action"=>"index", "controller"=>"invoices"}> did not match <{"controller"=>"invoices", "action"=>"create"}>, difference: <{"action"=>"create"}>.
Expected block to return true value.
So im trying to figure out why my form is doing a POST on INDEX and why my test thinks im doing an index route. I have tried inserting :method => :post in the test but it doesnt seem to work.
Have you tried this?:
assert_routing({ :path => "invoices", :method => :post },
{ :controller => "invoices", :action => "create" })
Ok, I'm relatively new to rails and am working through the book Rails for PHP programmers. To make things interesting I'm using Rails 3 and haml to work through the book, and the book is written using Rails 2.X and erb so some of the examples are outdated.
To the question:
The route example in the book is
map.presentation 'meetings/:meeting_id/presentations/:action/:id',
:controller => "presentations",
:action => "show",
:meeting_id => /\d+/
So that was a pre-Rails 3 route. My adaptation of the above into a Rails 3 route is:
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations#show', :constraints => {:id => /\d/}
My adaptation works for the destroy action, but not for the edit action... and due to my inexperience I don't know what I'm doing wrong. From the article here (http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/) it looks like I'm doing it right, but something isn't right.
The link_to helpers that produce the urls are as follows
= link_to 'edit', :controller => 'presentations',
:action => 'edit',
:meeting_id => presentation.meeting.id,
:id => presentation.id
# Incorrectly Produces: http://localhost:3000/presentations/edit?meeting_id=2&id=1
= link_to 'destroy', { :controller => 'presentations',
:action => 'destroy',
:meeting_id => presentation.meeting.id,
:id => presentation.id },
:confirm => 'Are you sure?',
:method => :delete
# Correctly Produces: http://localhost:3000/meetings/2/presentations/destroy/1
Your help would go a long way to clearing up my routing haziness. Thanks so much!
EDIT
Here's the non-working routes.rb file in its entirety:
UserGroup::Application.routes.draw do
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
EDIT 2
Thanks to codykrieger I took a look at the rest of the routes file (I know, duh right?). Apparently, all those get "..." routes are added when using the rails generator and help to make some default connections in the app. I commented out the get "presentations/edit" line and, wonder of wonders, my routing adaptation actually does work.
This works:
UserGroup::Application.routes.draw do
get "presentations/new"
#get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
I played around with the order of the routes in the file and also found that if I put my route above all the auto-generated routes, without commenting out the get "presentations/edit" line, my route still has the intended effect.
This also works:
UserGroup::Application.routes.draw do
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
#default route
match ':controller(/:action(/:id(.:format)))'
end
I'm thinking that the latter is the better way to go and I should make my custom routing declarations above the standard generated ones, but I'm not sure. If someone out there wants to comment on which practice is better, or if it's better to entirely remove the generated ones if I want to specify my own, I'd love to hear it.
Thanks all :)
Routing is done on a first match basis (top to bottom) in the routes.rb file.
So you can keep all routes as you have done above if you want, and Rails will use the first one that matches. However, its messy, potentially buggy and totally unnecessary. So the "match..." lines in your code should be enough, and you can remove the "get..." lines.
Furthermore, your "match...presentations...." line has a bug, it should be
...{:id => /\d+/}
else it will validate only 1-digit length ids.
Finally, as far as your overall routes.rb is concerned, you do not need to do
link_to 'edit', :controller => 'presentations',
:action => 'edit',
:meeting_id => presentation.meeting.id,
:id => presentation.id
Instead, you should use edit_meeting_path, new_meeting_path etc. For instance
<%= link_to "New meeting", new_meeting_path %>
<%= link_to "Edit meeting", edit_meeting_path(#meeting) %>
Finally, you should read Rails Routing from the Outside In for best practices on how to design nested routes etc.