In my Rails 2.3 application, the following routes were working properly
map.ajax 'ajax', :controller => 'widgetresponse_controller' , :action => 'getWidgetJson'
When I migrated to Rails 3,
I tried a number of new routes, to get this working but none of them worked.
1.
match 'ajax' => 'widgetresponse#getWidgetJson', :as => :ajax
2.
match 'ajax' => 'widgetresponse_controller#getWidgetJson', :as => :ajax
3.
get 'widgetresponse/getWidgetJson', :as => :ajax
4.
get 'widgetresponse/getWidgetJson'
Its a very basic question to ask, but I don't know what I am doing wrong.
You could try this:
match "/widgetresponse/getWidgetJson/:id" => "widgetresponse#getWidgetJson"
One thing that may have happened before is that things were falling through to the default route which was removed in Ruby on Rails 3 (good idea).
I found this guide:
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
really helpful with lot of yummy new rails 3 options.
Related
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'
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.
I'm migrating an application from Rails 2.3.5 to Rails 3.2.8.
I have this route from the Rails 2 app that is giving me headaches:
map.resources :soumission_vt,
:path_prefix => "/soumission/VT/:page_id", :as => 'police/:action/:id',
:requirements => {:page_id => /\S+/}
wich generates the following:
soumission_vt_index GET /soumission/VT/:page_id/police/:action/:id(.:format) {:controller=>"soumission_vt"}
POST /soumission/VT/:page_id/police/:action/:id(.:format) {:controller=>"soumission_vt"}
new_soumission_vt GET /soumission/VT/:page_id/police/:action/:id/new(.:format) {:controller=>"soumission_vt"}
edit_soumission_vt GET /soumission/VT/:page_id/police/:action/:id/:id/edit(.:format) {:controller=>"soumission_vt"}
soumission_vt GET /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
PUT /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
DELETE /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
I translated it this way in Rails 3:
scope '/soumission/VT/:page_id', :constraints => {:page_id => /\S+/} do
resources :soumission_vt, :as => 'police/:action/:id'
end
but I am getting an Invalid route name: 'police/:action/:id_index'...
So is there a way to reproduce those routes in Rails 3?
Thanks!
After one try, I succeeded to make it work with the following lines of code:
scope '/soumission/VT/:page_id' do
get 'police/new', controller: :soumission_vt, action: :new, as: :new_soumission_vt
end
Hope this will help you finish your migration in time !
;)
I have a an url like "http://domain.com/1and2" that I wanted to set up in config/routes.rb like this:
match "1and2" => "frontpage#oneandtwo"
(with controllers and views in place).
Running 'rake routes' outputs 'Invalid route name: '1and2''. This error is apparently triggered when you start a match with a numeric character.
Is there a workaround, or am I doing it wrong?
match '/:id' => "frontpage#oneandtwo", :constraints => {:id => /1and2/}
The root of the problem is that methods in Ruby cannot start with a number. Since Rails routing will generate an accessor method for each route, you'll get an error.
You can pass by the issue by naming your route differently with the :as parameter.
I had an issue where I wanted to redirect from a URI /2012 -- which resulted in an error. I corrected it by adding :as => current_year to the routing:
match "/#{Time.now.year}" => redirect("..."), :as => :current_year
Further information:
https://github.com/rails/rails/issues/3224
I'm getting this error:
uninitialized constant OpenidsController
I can't figure out why. I'm following this guide: http://www.danwebb.net/2007/2/27/the-no-shit-guide-to-supporting-openid-in-your-applications
I used the following command to generate the controller:
script/generate controller Openid new create complete
And I have put the following line in my routes file as the guide says to do:
map.resource :openid, :member => { :complete => :get }
Any ideas? I'm new to RoR so hopefully this is easy for someone else.
You can either change your route to this
map.resource :openid, :member => { :complete => :get }, :controller => 'openid'
or rename your controller class to OpenidsController.
One thing to take note of is that blog post is nearly 3 years old - you might want to consider other articles as well.
You might find this OpenID example helpful, though it's also a little dated at this point.