The action x could not be found for xyzController - ruby-on-rails

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.

Related

devise (No routes Matches [put] "/users/edit.user)

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.

No route matches [POST] "/sessions/user" with devise

When i try to log in using devise on my rails app, I get the following error.
No route matches [POST] "/sessions/user"
I have tried other solutions that I have found here but they don't seem to work.
My routes.rb has the following in the file.
Rails.application.routes.draw do
devise_for :users
resources :non_availabilities
resources :sessions
resources :programs
resources :activities
resources :locations
resources :employees
resources :expertises
resources :emp_classifications
resources :educations
get 'dashboard/index'
root 'dashboard#index'
end.
How do I fix this?
Can you add this line to your routes
match '/sessions/user', to: 'devise/sessions#create', via: :post
I faced the exact same situation (my app has a Session model and SessionsController too). This solution worked for me, with a tiny modification. The code below was added to config/routes.rb:
devise_scope :user do
match '/sessions/user', to: 'devise/sessions#create', via: :post
end

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'

Two routes with multiple parameters to single controller's method?

Let's assume I have models: Article, Post, and Comment. For these models I need to map CommentsController#show in articles and posts. Something like this:
resources :articles do
resources :comments, :only => [:show]
end
resources :posts do
resources :comments, :only => [:show]
end
And this works perfectly, i.e. it'll generate routes something like this:
/articles/:article_id/comments/:id
/posts/:post_id/comments/:id
Both pointing to the same controller's single method: CommentsController#show
What I need is to include more/multiple parameters for posts while retaining the same URL routes to CommentsController:
/articles/:article_id/comments/:id
/posts/:post_id/:post_title/:post_year/comments/:id
I tried match but first occurrence at resources :articles do... overrides it.
resources :articles do
resources :comments, :only => [:show]
end
match '/posts/:post_id/:post_title/:post_year/comments/:id', :to => 'comments#show', :as => :show_post_comments
It throws an error:
No route matches {:controller=>"comments", :action=>"show",
:post_id=>10, :post_title=>"some title", :post_year=>"1995", :id=>"1"}
So, is it possible to create such a route with multiple parameters to single controller's method in two resources using get or something?

How to change a route name rails 4

I changed the routing of posts#index to match blog and I now get /blog in the URL which I was trying to accomplish.
I've tried several different things to get my actual blog post which the route currently looks something like /posts/this-is-a-test to also use blog rather than posts in the URL.
Below is my current route file. I am using the friendly_id gem, if that makes any difference in answering this question.
resources :posts do
resources :comments
end
resources :contacts, only: [:new, :create]
root "pages#home"
get "/home", to: "pages#home", as: "home"
get "about" => 'pages#about'
get "pricing" => 'pages#pricing'
get "contact_us" => 'pages#contact_us'
match 'blog', to: 'posts#index', via: :all
end
path option along with resource must help.
resources :posts, :path => 'blogs' do
resources :comments
end
This will change all /posts and /post to /blogs/ and /blog.
If you want to change your route's helper methods such as posts_path to blogs_path and new_post_path to new_blog_path etc, you can change it with as tag.
resources :posts, :path => 'blogs', :as => 'blogs' do
resources :comments
end
Or yet better, you can specify the controller and route blogs directly as:
resources :blogs, controller: 'posts' do
resources :comments
end
This is the awesomeness of Rails! :)
match 'blog/:id' => 'posts#show'
should work. But if you want to match every method in posts controller to blog (and you don't want to use the posts path), I would just rename the controller to blog instead and add resource :blog in the routes.
This helped me, from official guides documentation, add this in your routes.rb file:
get '/patients/:id', to: 'patients#show', as: 'patient'

Resources