Rails 4 and Jammit routing issue - should not use match - ruby-on-rails

I've upgraded to Rails 4.0.5 and i'm using Jammit 0.6.6.
When starting the server i get an error :
/home/haimh/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.5/lib/action dispatch/routing/mapper.rb:191:in normalize_condition!:You should not use the match method in your router without specifying an HTTP method.
Looking at the stack trace i see that Jammit's routes.rb file is using the old routing API.
Is there any way of resolving this issue, beside updating the code manually in Jammit's routes.rb file?

use the via
like this:
adding , via => [:get, :post] to the end
match ':controller(/:action(/:id))' , :controller=> /admin\/[^\/]+/, :via => [:get,:post]
if you dont want to use match , you will have to have your routes inside the
resources :controller do
get :action, :on => :collection
end

Related

How to convert Rails dynamic routes for Rails 5.2

I have a legacy Rails app which started life in Rails 1.2.
It has been converted through to Rails 5.0 over the years
The routes.rb file contains only two lines of 'wildcard routes' as follows
match ':controller(/:action(/:id))',:constraints => {:controller => /admin\/[^\/]+/}, :via => :all
match '/:controller(/:action(/:id))(.:format)', :via => :all
Links within the application are codedas in the following example:
<%= link_to('Marital Status', {:controller => 'marital_status', :action => 'list'}) %>
It appears that this sort of route is deprecated and will be removed in Rails 5.2
My question is how can I convert the routes to something that is acceptable to Rails 5.2.
Bear in mind that the application has about 150 controllers with a corresponding large number of actions.
I am in a similar boat and for the time being I made a rake task to generate all of the routes and write them to the routes.rb file.
Using a little ruby meta-programming its pretty straight forward to find all the methods available to a class <controller>.instance_methods then filter out all of the built in rails methods + those inherited from the application controller.
update:
I used a rake task to generate routes and put the following code in the bottom of the routes file to log when a route was not found in production:
match '/:controller(/:action(/:id))(.:format)', :via => :all, :to => proc { |env|
route = env["action_dispatch.request.path_parameters"]
Rails.logger.error("******************************************************************************************************")
Rails.logger.error("ROUTE NOT FOUND. USING WILDCARD ROUTE. REQUIRED ROUTE IS:>")
Rails.logger.error("#{env['REQUEST_METHOD'].downcase} #{route[:controller]}/#{route[:action]} => #{route[:controller]}##{route[:action]}")
Rails.logger.error("******************************************************************************************************")
controller = eval("#{route[:controller].camelize}Controller")
action = route[:action]
controller.action(action).call(env)}
Because match method has been deprecated, use get for GET and post for POST.
eg. get '/list', to: 'marital_status#list'

Rails 4: You should not use the `match` method in your router without specifying an HTTP method

Okay, so I've upgraded to Rails 4 (kind of unplanned with my 10.9 server update) and have been able to get everything running on my photo gallery app except for the routes. For some reason I've always had trouble understanding routes since rails 3. Here was my previous working code under Rails 3
root :to => "gallery#index", :as => "gallery"
get 'gallery' => 'gallery#index'
resources :galleries
match 'gallery_:id' => 'gallery#show', :as => 'gallery'
I understand that match has been depreciated, but if I try to use GET, I'm getting the following error:
Invalid route name, already in use: 'gallery' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.
Basically, I want the root (index) to load as "/photos/gallery" as it does, and my show action to load, for example, record id 435 as: "/photos/gallery_435" which is how I previously had it working. Sorry for what is probably a simple question, I just cannot seem to grasp the rails routing.
Try this
match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'
You can then refer to this path as gallery_show_path in your helpers and views.
Changing the 'as' removes the conflict.

Ruby on Rails 4.0.0 | Routing changes from Ruby on Rails 3.0

So i have been watching a video which was introducing me to ruby on rails but the guy in the video is using ruby on rails 3 (and I am using ruby on rails 4.0.0) and when I try to use the code:
match ':controller(/:action(/:id(.:format)))'
It denies it and displays this...
You should not use the match method in your router without specifying an HTTP method.
If you want to expose your action to both GET and POST, add via: [:get, :post] option.
If you want to expose your action to GET, use get in the router:
However I don't quite understand what it was asking me here and when i put this in the routes file
match 'new', to: 'episodes#new', via: [:get, :post]
It was unable to actually find the page i was looking for. Does anyone know what I'm supposed to type instead of
match ':controller(/:action(/:id(.:format)))' for Ruby on rails 4.0.0?
Thanks.
match '/new', to: 'episodes#new', via: [:get, :post]
I think you forgot a "/". BTW, it would be helpful to be more elaborate about "not finding" the page. Any error message?
And for understanding why you should specify HTTP methods for routing:
http://guides.rubyonrails.org/routing.html
match ':controller(/:action(/:id))(.:format)'
works for rails 3.0, but for 4.0 the missing link is the via: [:post, :get] statement as indicated in your rails command line.
match ':controller(/:action(/:id))(.:format)' , via: [:post, :get]
works great and keeps your code DRY by not re-editing your routes folder every time you generate a page.
This is the solution to your problem:
match ':controller(/:action(/:id))(.:format)' , via: [:post, :get]

Rails 3.2.3 - how to setup routes

Here are the url I need to be in my Rails application
site.com
site.com/page/11
site/tags/my_tag
site/tags/my_tag/page/123
where page and tags are actions of HomeController. So how must the file routes.rb look like in this case?
You probably should start with:
match '/page', :to => 'home#page'
match '/tags', :to => 'home#tags'

Rails implement an API using Routes pointing to existing controller actions

I have a Rails app that does everything I need it to do via a HTML interface, now I'd like to bolt on an API providing access to bits of the functionality.
How would I do this selective forwarding of some API controller actions to another controller's actions using the Routes.rb?
I have tried the following:
My regular controller routes work fine using:
match 'stuff' => 'stuff#index'
get 'stuff/index'
get 'stuff/some_get_action'
post 'stuff/some_post_action'
But then when I try for my API:
match 'api' => 'api#index'
match 'api/some_get_action' => 'stuff#some_get_action', :via => :get
match 'api/some_post_action' => 'stuff#some_post_action', :via => :post
or...
match 'api' => 'api#index'
get 'api/some_get_action', :to => 'stuff#some_get_action'
post 'api/some_post_action', :to => 'stuff#some_post_action'
I get an error. When I navigate to /api/index to server a HTML page that contains forms for testing the API URLs the url_for raises a 'Routing error' exception saying 'No route matches...'.
You may want to include ':as => ' and define your route names that you may be using as your link paths.
get 'api/some_get_action' => 'stuff#some_get_action', :as => 'some_get_action'
and the link path in your index file will have 'some_get_action_path'. Not sure that 'match' or 'get' automatically resolves to a path name, which by setting ':as' it definitely will.
I like your idea for setting up this page for testing. I'm always doing it in the console, which is surely more difficult than simply clicking a link. Your links probably need to infer that they are :json requests, not :http.

Resources