devise (No routes Matches [put] "/users/edit.user) - ruby-on-rails

ruby 2.3.3,
rails 5.1.6.1
I am trying to edit a user, after i click on the update, my app loads to this:
No route matches [PUT] "/users/edit.user"
# routes.rb
Rails.application.routes.draw do
devise_for :users
resources :posts
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
get '*path' => redirect('/')
end

seems to be a problem with your routes.rb file
check this guide -> https://guides.rubyonrails.org/v5.0/routing.html#resources-on-the-web
from what I can tell you are not defining the routes pointing to your UsersController (devise_for :users doesn't do that, it defines completely other set of routes)
your routes.rb should look roughly like below:
Rails.application.routes.draw do
resources :users
...some other routes (like devise_for :users)
end
resources :users will define routes like /users for showing all user resource or /users/:id/edit for editing the user resource.
Now that you have those routes the will point to the UsersController class which should be defined in the users_controller.rb. In the controller you should define appropriate actions (like edit or update) that will update your user resource
I hope this is going to help solving your problem.

Related

Rails nested resources ignore single route

I have a rails nested resource in my routing.
i.e
resources :users do
resources :accounts
end
resources :accounts
The listing operations of course will be:
GET /users
GET /users/:user_id/accounts
I want to get rid of the /users route but retain the /users/:id/accounts route.
Any idea how I can go about this? Thanks
Using except: [:index] not will restrict both routes. Thats a nonsenical claim that can easily be refuted by just running rails routes. None of the options for resources "trickle down" to nested calls.
resources :users, only: [] do
resources :accounts, only: :index
end
only: [] skips generation of all the "user" routes.
This will generate the routes:
Prefix Verb URI Pattern Controller#Action
user_accounts GET /users/:user_id/accounts(.:format) accounts#index
# ...
Note that the param key is :user_id and not :id. If you REALLY want to break the conventions you would need to do:
# don't do this - its stupid
scope '/users/:id', as: :user do
resources :accounts, only: :index
end
let set only: [] then rails routes will generate /users/:id/accounts as you want
resources :users, only: [] do
resources :accounts # , only: [:index] if you just only keep users/:id/accounts
end
# if you only want to get rid of GET /users
resources :users, except: [:index]
# if you mean you want to get rid all of /users routes (not just only GET /users) then comment above line
This is what scope is for
scope :users do
resources :accounts
end
Rails guides on routing
Or you can use these same two ways, use namespace with scope:
namespace :users do
scope ':user_id' do
resources :accounts
end
end
Use just only scope:
scope ':users/:user_id' do
resources :accounts
end

Rails routing issue: Unwanted route

I have a Ruby on Rails project. In my config/routes.rb, I have the following setup:
scope module: :comments do
resources :tasks do
resources :comments, only: %i[index]
end
end
This creates the route that I want in rake routes:
v1_task_comments GET /v1/tasks/:task_id/comments(.:format) v1/comments/comments#index
but it also creates the following route, which is wrong and which I don't want:
v1_tasks GET /v1/tasks(.:format) v1/comments/tasks#index
How do I create the first route without generating the second?
You can use the only option of resources routes helper:
resources :tasks, only: :none do
# ...
end

The action x could not be found for xyzController

I'm a beginner in Ruby on Rails and I have a problem. I'm trying to add comments to my app. Everything is working, but when I added this code to routes.rb
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
I can't update any gallery or article. My whole routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
root 'public#index'
end
It looks like the request (articles/12?page_id=4) is falling through to the match statement and not getting picked up by your resources. Your match statement:
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
is matching "articles" as the controller, and looking for "12" as the action and it clearly can't find it.
I would scrap the match statement and go strictly with named routes or resourceful routing permanently, or at least while debugging. Next, run
rake routes
and check the results. From your routes.rb I am guessing you will have a line like this:
article GET /articles/:id(.:format) articles#show
If that is indeed the case, make sure you have the show() method defined in the articles controller, and verify that the logs are showing a GET request for it.

Invalid route name, already in use: 'user'

I am trying to add followers to my rails application. I am seeing the following error when I run rails s to start my server:
Invalid route name, already in use: 'user' (ArgumentError)
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. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Routes
Rails.application.routes.draw do
devise_for :users
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
resources :posts do
member do
post '/like' => 'posts#like'
end
end
get ':username' => 'users#show', as: 'user'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
It may be helpful to know that I added:
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
Before I added the above, my rails app worked.
I'm sure it's probably a simple fix, but I am new to rails. I've been tinkering with routes and searching online for over an hour, trying to understand and solve the problem. I'm hoping someone more knowledgeable can guide me in the right direction.
Since you are using devise there are some routes already created by devise_for users so avoid using such routes which has conflict with already defined routes. Instead of using as: :user put another relevant name like as::user_profile
Hope it helps
In your case, Devise routes and Users routes are conflicting. You need to change one of them. You can either put Devise under a specific prefix like,
devise_for :users, :path_prefix => 'auth'
resources :users
Reference: https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface
Please do not forget, that there is "as" option too.
devise_for :users, as: 'some_unique_prefix'

Nested Routing - current_user in URL

I have to models User and Blog. Their respective urls are:
test.com/u/user_name # User
test.com/blogs # Blog
I'm trying to achieve that the blogs are nested to the user which created it. E.g. test.com/u/user_name/blogs and for an article test.com/u/user_name/blogs/article_name.
But the index for all blogs should respond to: test.com/blogs.
right now my routes are like this:
resources :users do
resources :blogs, except: [:index]
end
resources :blogs, only: [:index]
Which doesn't work.. What am i missing?
Routes
#config/routes.rb
resources :blogs, only: :index #-> domain.com/blogs -> blogs#index
resources :users, path: "u" do #-> domain.com/u/:id
resources :blogs #-> domain.com/u/:user_id/blogs/:id
end
You'll want to look up about the path argument for your routes
As a rule of thumb, you'll want to include any "non conventional" paths at the bottom of the routes file. As the routes are determined from the top to bottom, if you're capturing obscure paths near the top of the file, you'll end up causing conflict with your other routes

Resources