I'm trying to implement a "à la github" url like "github.com/josevalim/inherited_resources".
I achieved my goal with this code :
resources :users, :path => '', only: [] do
resources :projects, :path => '', only: [:show]
end
But now, I can't find a way to redirect properly to this route.
When I run a rake routes in the console, all I have is
GET /:user_id/:id(.:format) projects#show
and no name to find a helper method like my old users_project_path.
Have I miss something ? Thanks!
I recently did something similar, try the following
scope '/:user_id', as: :user do
resources :projects, path: '', only: [:show]
end
Running rake routes should then give you
user_project GET /:user_id/:id(.:format) projects#show
and this will give you the proper user_project_path(:user_id, :id) route helper.
You're deleting the path for this reason you can get route_path, use as to establish a route name.
resources :users, :path => '', only: [] do
get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user
end
OR if you don't want use that you can use this code for generating path:
url_for([#user, #project])
Related
I get the following clicking that link
<%= link_to "(#{User.count_friend_requests(current_user)}) Friends Requests", :controller => "users", :action=>"friend_requests"%>
i get that error
ActionController::UrlGenerationError in Devise::Registrations#edit
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
routes
devise_for :users, :path_prefix => 'my'
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
users_controller
def friend_requests
#frnds = User.find_friend_requests(current_user)
end
Heh, you've mounted devise with prefix /my/, so your routes are like
/my/users/..
and resources :users mounts your code without any prefix.
You should remove prefix my or or change your resources to
scope "my" do
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
end
to get path like /my/users/friend_requests/
Also I see that rails looks for your method in devise/users (not in your users) controller
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
If you want to patch it, you should take a look how to patch a devise's users controller.
How to rename index path only?
routes.rb
resources :tasks, :except => [:create] do
collection do
..............
end
member do
.............
end
end
instead of /tasks in URL I need /trigger or even /tasks/trigger will do, im on rails3.
I tried
1.
collection do
'/', to: 'tasks#trigger'
end
and
2.
resources :tasks, :except => [:create] do
get '/task', to: 'task#trigger', as: task_index
end
both throw up errors.
any ideas?
For URL like '/trigger' add to routes.rb:
get 'trigger' => 'tasks#index'
For URL like '/tasks/trigger' modify routes.rb:
resources :tasks, :except => [:create] do
collection do
get 'trigger' => 'tasks#index'
end
end
If your index method in task's controller named as 'trigger, 'tasks#index' is replaced by 'tasks#trigger'
If you want to make your own index route then you need to do two things
1- you need to disable index path created by resources
2- Define your own path whatever you want.
As per your requirement you want to make index route as '/trigger'.
Following is the solution.
get 'trigger' => 'tasks#index'
resources :tasks, :except => [:create, :index] do
collection do
..............
end
member do
.............
end
end
Just restraint index route from being generated in your resources call:
resources :tasks, :except => [:create, :index]
And then do your own custom route. For '/trigger':
get 'trigger', to: 'task#trigger', as: 'trigger
I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get
In routes.rb:
scope "(:locale)", locale: /en|de/ do
get 'service' => 'service#index'
get 'service/:id' => 'service#show'
end
Then in view I use helper service_path(params[:locale], id) and get this link /en/service.1.
But i need link like this /en/service/1.
Check the routing via rake routes:
service GET (/:locale)/service(.:format) service#index (locale=>/en|ru/}
GET (/:locale)/service/:id(.:format) service#show {:locale=>/en|ru/}
How to get normal links like /en/service/1, what I'm doing wrong?
You should rename your controller to pluralize form Services
Change in routes:
get 'service' => 'service#index' to get 'services' => 'services#index', :as => :services
and
get 'service/:id' => 'service#show' to get 'services/:id' => 'services#show', :as => :service
Or you can write simple:
resources :services, :only => [:show, :index]
I want to route http://localhost:3000/users/1/rename/alex to my users controller with rename action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename', but this is not working, the part after 'users/:id/' is not mapped at all, since I cannot get name by params[:name]
Update:
In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users, put your match line before it.
Alternatively, you can pass a block to resources:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end