Users can view and edit account information in my app using UsersController
This controller only ever shows information about the current user.
I want to adjust the routes so that:
get /account accesses UsersController#show, user_id is retrieved from a current_user variable
get /account/edit similarly accesses UsersController#edit
put /account/ will hit UsersController#update, again using current_user instead of an ID
Basically I want to refer to UsersController as 'account' in my URLs and I don't want to use IDs because I'm always just using the current user. I also don't want URLs like /users/1 to function at all.
How can I achieve this?
I think that you are talking about singular resources.
http://guides.rubyonrails.org/routing.html#singular-resources
# Not sure if you need this
resource :users, :only => [:index, :create, :destroy]
# This is what you are looking for
resource :user, :as => :account, :only => [:show, :edit, :update]
Update
Since you need "/account" instead of "/user", you should do
resource :account, :controller => :users, :only => [:show, :edit, :update]
Given your setup, you probably have something like this in your routes.rb:
resources :users
You should be able to just change it to this:
resources :users, :as => "account"
I haven't tested it but this should work. For more info, check the Rails guides on Routing.
Two ways to do it:
In your route.rb:
resources :users, :as=>'accounts'
In your route.rb, remove resources :users and add:
match '/account'=>'users#show'
match '/account/edit'=>'users#edit'
match '/account/'=>'users#update'
Related
I’ve this on routes.rb
resources :questions, except: [:show] do
get '/resource/:subject/:id', to: 'resource#show', as: "resource", param: [:name, :id]
It says that:
Invalid route name, already in use: ‘resource' 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
I know that resources create two routes with the same path, show and destroy both uses resource_path, how does it is being created internally? and how i can generate my route for show wihtout overwrite the one in destroy?
A good way to eliminate routes unneeded is by specifying the :only option
resources :user, :only => [:edit]
instead of
resources :user, :except => [:new, :create, :edit, :update, :show, :destroy]
It seems to me that you could take out show and then define the route that you want separately. See if this works:
resources :questions, except: :show
get '/resource/:subject/:id',
to: 'resource#show',
as: "resource", # This is where the error is.
param: [:name, :id]
EDIT: Ah, yes. The :as parameter needs a different name. This will work:
resources :questions, except: :show
get '/resource/:subject/:id',
to: 'resource#show',
as: "resource_show",
param: [:name, :id]
I have a polymorphic model Discussion. It can be applied to Specialty model and Program model. My routes are set up as:
resources :programs, :only => :show do
resources :discussions, :only => [:show, :create, :destroy, :new]
end
resources :specialties do
resources :discussions, :only => [:show, :create, :destroy, :new]
end
So, new discussions are made either like:
/specialties/yyyyy/discussions/new
/programs/yyyyyy/discussions/new
The problem is in my discussions_controller.rb file. I have the function:
def new
#object = xxxxx.find(params[:id])
end
How do I choose the appropriate model for the form (eg. to replace 'xxxxx') and for determining the discussionable_type. I would assume I could parse the URL, but it doesn't seem clean. Any ideas?
Given your routes, you should either have params[:program_id] or params[:specialty_id] (or alike).
This will tell you what to use.
I have a rails app where Users can create Programs and other things yet to come, we'll say Plans (hypothetically).
Users can also like Programs and Plans. (Like is polymorphic)
I am trying to figure out routes so I can see the Programs a User creates, all the things a User likes, or just Programs or just Plans a User likes.
I have these routes, but this doesn't seem to be right.
resources :users, :only => [:index,:show] do
resources :programs, :only => [:index]
resources :likes, :only => [:index] do
resources :programs, :only => [:index]
end
end
The route for programs that a user likes requires a URL like: users/user_id/likes/like_id/program.
How do I create a URL like: users/user_id/likes/programs and get all programs that are liked.
I am using the Socialization Gem which has a method for "likeables_relation(Class)" which returns a relation of whatever class is requested. I just need help with the routing.
I've just tried with the following routes:
resources :users, :only => [:index,:show] do
resources :programs, :only => [:index]
resources :likes, :only => [:index] do
collection do
get :programs
end
end
end
and the
http://localhost:3000/users/1/likes/programs
works fine.
It required adding a new action 'programs' to the LikesController class.
This is what rake routes returns:
programs_user_likes GET /users/:user_id/likes/programs(.:format) likes#programs
I've setup a simple app and added a scaffold to do some of the work for me (I'm a noob).
resources :cars
How do I remove certain actions from the routes? And remove the corresponding urls?
For example I want to keep the 'show' and 'edit' actions & urls.
But I don't want there to be a 'new' 'index' or 'delete'
I understand this is probably a really simple question, but I've not been able to find an answer.
resources :cars, :except => [:new, :index, :delete]
or
resources :cars, :only => [:show, :edit]
Also take a look at Rails Guides
If you want to remove some actions then you can mention action name in the array
resources :photos, only: [:index, :show]
But
if you want to disable all the default CRUD actions then you should use
resources :photos, only: []
I have a projects controller/model. Instead of listing projects on the #index page, I show a list of drop downs, which submits to projects#select, which finds the right Project (I've made sure there can only be 1 for each combination of options) and forwards the user to the #show page for that Project.
So for my routes I do this...
resources :projects, :only => [:index, :show] do
collection do
get 'select'
end
end
And thats fine, but the helper method for #select is 'select_projects', which is understandable but in my case I really want 'select_project'. And I really don't want to alias this in another file. No problem I can use :as...
resources :projects, :only => [:index, :show] do
collection do
get 'select', :as => 'select_project'
end
end
But now my helper is 'select_project_projects'. So I cheat a little (still better than aliasing in another file)...
resources :projects, :only => [:index, :show]
match '/projects/select', :to => 'projects#select', :as => 'select_project'
This looks like it might work, but it doesn't because /project/select actually matches the route for 'project#show'. Changing the order of the lines does the trick.
match '/projects/select', :to => 'projects#select', :as => 'select_project'
resources :projects, :only => [:index, :show]
But is there a more elegant way of handling this? I realize this is borderline OCD, but I'd like to be able to have complete control over the route name within the resources block.
use resource instead of resources
you probably don't want to make it a collection route but a member route:
resources :projects, :only => [:index, :show] do
member do
get 'select'
end
end
This way you'll have the select_project helper.
For those that want to rename the helper method side of things (as the title suggests):
resources :posts, as: "articles"