Turning resources into custom routes (Ruby/rails) - ruby-on-rails

Started building an application here. client and server style architecture sending active resources across the wire and storing as activeRecord server side. Managed to get it up and running with a nice example in an O Reilly book except its using scaffold.
Rails routing - custom routes for Resources
is using map.resources from rails 2-
I'm using rails 3 so Its not really applicable and while I did post a question about routes from 2 to 3, I still cant convert this.
So here whats im looking at:
Rake routes with
resources :user_requests
gives:
user_requests GET /user_requests(.:format) {:controller=>"user_requests", :action=>"index"}
POST /user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
new_user_request GET /user_requests/new(.:format) {:controller=>"user_requests", :action=>"new"}
edit_user_request GET /user_requests/:id/edit(.:format) {:controller=>"user_requests", :action=>"edit"}
user_request GET /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
PUT /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"update"}
DELETE /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"destroy"}
I'd like to remove this and the resources and have my own routes pointing to my own defs.
Heres a quick attempt
match '/user_requests(.:format)' => 'user_requests#create , :via =>:post'
match '/user_requests/:id(.:format)' =>"user_requests#show"
returns almost the exact same as above
/user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
/user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
With the exception of the REST nouns at the start and the links. Its the same yet my own routes dont work.
What do I need to add to my routes to make them do the same thing as resources?
I'm not keeping scaffold as I've been told its never used in the real world. And I will be changing the names of my defs, but one step at a time.
Error that server shows:
Started POST "/user_requests.xml" for 127.0.0.1 at Tue Jul 12 17:13:32 +0100 2011
Processing by UserRequestsController#create as XML
Parameters: {"method"=>"POST", "user_request"=>{"depth"=>3000000, "url"=>"www.stackoverflow.com"}}
SQL (0.1ms) SELECT 1 FROM "user_requests" WHERE ("user_requests"."url" = 'www.stackoverflow.com') LIMIT 1
AREL (0.3ms) INSERT INTO "user_requests" ("updated_at", "depth", "url", "created_at") VALUES ('2011-07-12 16:13:32.765392', 3000000, 'www.stackoverflow.com', '2011-07-12 16:13:32.765392')
Completed 404 Not Found in 17ms
ActionController::RoutingError (No route matches {:controller=>"user_requests", :id=>#<UserRequest id: 6, url: "www.stackoverflow.com", depth: 3000000, created_at: "2011-07-12 16:13:32", updated_at: "2011-07-12 16:13:32">, :action=>"show"}):
app/controllers/user_requests_controller.rb:19:in `create'
app/controllers/user_requests_controller.rb:16:in `create'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

It would guess that line 19 of your UserRequestsController has something like
redirect_to #user_request
which tries to guess a URL for showing a UserRequest object. Rails no longer knows how to do this; you've lost the helper methods generated by resources (such as user_request_path, new_user_request_path, etc).
You can tell Rails to generate the "show" helper method by adding a :as option to your show route, without the _path postfix:
match '/user_requests/:id(.:format)' => "user_requests#show", :as => 'user_request'
You'll now have access to a user_request_path and user_request_url, which Rails can use to find the URL to "show" a UserRequest.

Related

Rails route not found even when its present

My following API route is not found when hit with CURL or POSTMAN client
Started POST "/api/users/register_handheld" for 181.74.100.34 at 2017-05-11 11:27:42 +0000
DEBUG: Chewy strategies stack: [2] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
ActionController::RoutingError (No route matches [POST] "/api/users/register_handheld"):
But when I run rake routes, this URL is present.
api_users_register_handheld POST /api/users/register_handheld(.:format)
crowd/api/v1/handheld_users#create {:format=>"json"}
This URL actually works if I hit in rails console:
app.post "/api/users/register_handheld"
Started POST "/api/users/register_handheld" for 127.0.0.1 at 2017-05-11 11:21:03 +0000
DEBUG: Chewy strategies stack: [3] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
Processing by Crowd::Api::V1::HandheldUsersController#create as JSON
Routes file is
namespace :api, defaults: {format: 'json'} do
scope module: :v1 do
resources :dashboard, controller: "account/dashboard"
post '/account/settings/create_notification' => 'account/settings#create_notification'
post '/users/register' => 'users#create'
post 'users/register_handheld' => 'handheld_users#create'
put 'users/update_profile' => 'handheld_users#update_profile'
post 'users/login' => 'handheld_users#login'
NOTE: this repo is Rails Engine
try below code:
/api/users/register_handheld.json
as you specified format type json
Maybe this helps someone.
Using POSTMAN i accidentally uncheck Host Key in headers (POSTMAN autogenerate it each time you send any request). It was a nightmare to find what's the problem until i compared my request with a similiar one and found the missing key.

Change redmine routes

www.redmine.org
We use Redmine for asset tracking and I would like to update the URLs. Presently the URLs revolve around issues and does not look right for assets.
I would like to update it so that I would navigate to:
www.domain.com/assets instead of www.domain.com/issues
Using:
Route alias in Rails
I have looked at routes.rb and changed the line:
resources :issues do
to
resources :issues, :path => :assets do
Unfortunately it doesn't work...
**rake routes
rake routes | grep issues
/home/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/htmlentities-4.3.1/lib/htmlentities/mappings/expanded.rb:465: warning: key "inodot" is duplicated and overwritten on line 466
[DEPRECATION] `last_comment` is deprecated. Please use `last_description` instead.
preview_new_issue GET|POST|PUT|PATCH /issues/preview/new/:project_id(.:format) previews#issue
preview_edit_issue GET|POST|PUT|PATCH /issues/preview/edit/:id(.:format) previews#issue
preview_issue GET|POST|PUT|PATCH /issues/preview(.:format) previews#issue
auto_complete_issues GET /issues/auto_complete(.:format) auto_completes#issues
issues_context_menu GET|POST /issues/context_menu(.:format) context_menus#issues
issue_changes GET /issues/changes(.:format) journals#index
quoted_issue POST /issues/:id/quoted(.:format) journals#new {:id=>/\d+/}
project_gantt GET /projects/:project_id/issues/gantt(.:format) gantts#show
issues_gantt GET /issues/gantt(.:format) gantts#show
project_calendar GET /projects/:project_id/issues/calendar(.:format) calendars#show
issues_calendar GET /issues/calendar(.:format) calendars#show
project_issues_report GET /projects/:id/issues/report(.:format) reports#issue_report
project_issues_report_details GET /projects/:id/issues/report/:detail(.:format) reports#issue_report_details
new_issues_import GET /issues/imports/new(.:format) imports#new
POST /issues/:object_id/watchers(.:format) watchers#create {:object_type=>"issue"}
DELETE /issues/:object_id/watchers/:user_id(.:format) watchers#destroy {:object_type=>"issue"}
project_copy_issue GET /projects/:project_id/issues/:copy_from/copy(.:format) issues#new
project_issues GET /projects/:project_id/issues(.:format) issues#index
POST /projects/:project_id/issues(.:format) issues#create
new_project_issue GET /projects/:project_id/issues/new(.:format) issues#new
project_issues_new POST /projects/:project_id/issues/new(.:format) issues#new
edit_issue PATCH /issues/:id/edit(.:format) issues#edit
bulk_edit_issues GET|POST /issues/bulk_edit(.:format) issues#bulk_edit
bulk_update_issues POST /issues/bulk_update(.:format) issues#bulk_update
report_issue_time_entries GET /issues/:issue_id/time_entries/report(.:format) timelog#report
issue_time_entries GET /issues/:issue_id/time_entries(.:format) timelog#index
POST /issues/:issue_id/time_entries(.:format) timelog#create
new_issue_time_entry GET /issues/:issue_id/time_entries/new(.:format) timelog#new
issue_relations GET /issues/:issue_id/relations(.:format) issue_relations#index
POST /issues/:issue_id/relations(.:format) issue_relations#create
issues GET /issues(.:format) issues#index
POST /issues(.:format) issues#create
new_issue GET /issues/new(.:format) issues#new
GET /issues/:id/edit(.:format) issues#edit
issue GET /issues/:id(.:format) issues#show
PATCH /issues/:id(.:format) issues#update
PUT /issues/:id(.:format) issues#update
DELETE /issues/:id(.:format) issues#destroy
issues_new POST /issues/new(.:format) issues#new
DELETE /issues(.:format) issues#destroy
POST /projects/:id/repository/:repository_id/revisions/:rev/issues(.:format) repositories#add_related_issue
DELETE /projects/:id/repository/:repository_id/revisions/:rev/issues/:issue_id(.:format) repositories#remove_related_issue
POST /projects/:id/repository/revisions/:rev/issues(.:format) repositories#add_related_issue
DELETE /projects/:id/repository/revisions/:rev/issues/:issue_id(.:format) repositories#remove_related_issue

rails routing duplicates path

I have a simple enough problem of taking a rails 2 route and making it working with rails 4. The rails 2 version is this:
map.token '/sessions/token', :controller => 'sessions', :action => 'token'
I've changed it to:
get '/sessions/token', to: 'sessions#token', as: '/token'
for rails 4. The problem is when I go to /sessions/token it immediately redirects to /sessions/token/sessions/token and gives a 404
I added byebug to the token method but the request never makes it there before redirecting. I added it early in the application controller but I reach a point where the next function just stops working.
The log produces this when requesting https://delete-me.domain.org/sessions/token:
[delete-me] Started GET "/sessions/token" for 127.0.0.1 at 2015-10-02 05:44:27 -0500
[delete-me] Processing by SessionsController#token as HTML
[delete-me] Redirected to http://delete-me.domain.org/sessions/token/sessions/token
[delete-me] Filter chain halted as :ensure_proper_protocol rendered or redirected
[delete-me] Completed 302 Found in 114ms (ActiveRecord: 5.8ms)
I think it gives the :ensure_proper_protocol error because it redirects to http from https. I don't know why it does that. Maybe someone has had a similar problem and they can enlighten me.
#config/routes.rb
resources :sessions do
get :token, on: :collection #-> /sessions/token (sessions#token controller action)
end

Change Root in ActiveAdmin

right now my active_admin gem in Rails has a default url of
www.home.com/admin
however the active admin resides in
www.home.com/group/admin
How can I make it so that every link in active admin will route to /group/admin/[next link]
I tried Gmat's solution below and this is what happened:
new_group_admin_user_session GET /group/admin/login(.:format)
active_admin/devise/sessions#new
group_admin_user_session POST /group/admin/login(.:format)
active_admin/devise/sessions#create
destroy_group_admin_user_session DELETE|GET /group/admin/logout(.:format)
active_admin/devise/sessions#destroy
group_admin_user_password POST /group/admin/password(.:format)
active_admin/devise/passwords#create
new_group_admin_user_password GET /group/admin/password/new(.:format)
active_admin/devise/passwords#new
edit_group_admin_user_password GET /group/admin/password/edit(.:format)
active_admin/devise/passwords#edit
PUT /group/admin/password(.:format)
active_admin/devise/passwords#update
group_admin_root /group/admin(.:format) group/admin/dashboard#index
group_root /group(.:format) group/dashboard#index
batch_action_group_admin_leads POST /group/admin/leads/batch_action(.:format)
group/admin/leads#batch_action
group_admin_leads GET /group/admin/leads(.:format)
group/admin/leads#index
POST /group/admin/leads(.:format)
group/admin/leads#create
new_group_admin_lead GET /group/admin/leads/new(.:format) group/admin/leads#new
edit_group_admin_lead GET /group/admin/leads/:id/edit(.:format) group/admin/leads#edit
group_admin_lead GET /group/admin/leads/:id(.:format) group/admin/leads#show
PUT /group/admin/leads/:id(.:format)
group/admin/leads#update
DELETE /group/admin/leads/:id(.:format)
capstonemlg/admin/leads#destroy
group_admin_dashboard /group/admin/dashboard(.:format)
group/admin/dashboard#index
I apologize for the formatting. The point is that all the links www.home.com/group/[anything] is broken.
Try this:
# config/routes.rb
namespace :group do
ActiveAdmin.routes(self)
end
I think you need to customize the ActiveAdmin namespace as well.
http://www.activeadmin.info/docs/2-resource-customization.html
ActiveAdmin.register Post, :namespace => "groups/admin"

Rails routing error on valid route

I have the following in my routes file:
resources :timelogs do
member do
post :stop
end
collection do
get :start
end
end
which produces the following on 'rake routes' :
rake routes | grep stop
stop_timelog POST /timelogs/:id/stop(.:format) {:action=>"stop", :controller=>"timelogs"}
However, when posting a request to that URL I'm seeing:
Started POST "/timelogs/325/stop" for 188.220.17.64 at Wed Nov 24 02:22:22 -0800 2010
ActionController::RoutingError (No route matches "/timelogs/325/stop"):
All of this looks like it should be working, however, it's not. What could be the problem here?
I see no problem with the routes you've pasted and have verified that they work for me in an scratch app.
Started POST "/timelogs/123/stop" for 127.0.0.1 at 2010-11-24 11:49:25 +0000
Processing by TimelogsController#stop as */*
Parameters: {"a"=>"b", "id"=>"123"}
Rendered text template (0.0ms)
Completed 200 OK in 60ms (Views: 59.9ms | ActiveRecord: 0.0ms)
Perhaps something else in your routes.rb is in conflict here?
Actually when you are trying to send your form with exists resource (ticket) rails by default will send PUT request, so you should set :method => :post clear or change route from
post :resolve, :on => :member
to
put :resolve, :on => :member

Resources