Rails - missing routes helper prefix - ruby-on-rails

I am missing the contacts#show prefix 'contact' as seen below.
rake routes
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
I am thinking this is the reason why I am getting a dot instead of a slash when clicking the following link.
_contact.html.erb
<%= link_to "delete contact", contact, method: :delete,
data: { confirm: "You sure?" } %>
The server logs the proper DELETE request, however, cannot render /contact.26 instead of the correct /contacts/26.
Started DELETE "/contact.26" for 128.177.12.30 at 2016-04-13 21:04:30 +0000
ActionController::RoutingError (No route matches [DELETE] "/contact.26"):
Every post I've come across with a dot instead of a slash seems to stem from a pluralization error, however, I don't believe this is the case here.
In addition, I've removed resources :contacts from my routes file, run $ rake routes, added resources :contacts, run $ rake routes, and the problem persists.
This problem seems to be unique to the contacts model, as the rest of my models aren't missing any prefixes or having this error when deleting.
How do I add the 'contact' prefix back to 'contacts#show'?
routes.rb file for reference:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'newevent' => 'events#new'
get 'newteam' => 'teams#new'
get 'newperformance' => 'performances#new'
get 'newhotel' => 'hotels#new'
get 'newcontact' => 'contacts#new'
get 'newflight' => 'flights#new'
get 'newground' => 'grounds#new'
get 'newguest' => 'guests#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :events
resources :teams do
member do
get :events
end
end
resources :performances
resources :hotels
resources :contacts
resources :flights
resources :grounds
resources :guests
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end

you may have a conflict between the routes set by
resources :contacts
and
get 'contact' => 'static_pages#contact'
The easiest solution is probably to change the static page's links. Best to keep the resources together if you can.

I just came across this same error.
the fix to my issue was that 's' at the end of resources
I had 'resource', and getting the same problem as you.
I changed it to 'resources' and it fixed my problem
in my case was just a typo.

This error showed up for me yesterday. What fixed it was deleting helper files that were not being used. Might be worth a try for you.

Related

No route error for edit helper that uses a token as an :id (From Ruby on rails tutorial third edition)

A url helper I am using does not work, even if it shows at rake routes. Not sure why, hopefully someone could give me a suggestion or point out errors. Something to do with using a token as an id or configuration?
Clicking on this:
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
Gives this error message (email parameter does pass through):
No route matches [GET] "/password_resets/$2a$12$Be6H4xiPjqlBtLxAozB7EujVy.2nZSLFJzL3LYDugvDmnn6aoGJ0G/edit"
The :new, :create paths work but not the :edit.
routes.rb (The last line):
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'members#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :members
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
This is from the Ruby on Rails Tutorial Third Edition by Michael Hartl (chapter 10)
Thank you for your time reading this and all answers will be appreciated.
The issue here is that -
Your edit_password_reset route is
/password_resets/:id/edit
but while calling it you are not passing the id but calling it on the member token like this -
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
So for the above call to work your edit route should be something like
/password_resets/<token>/edit
The new and create paths work because they don't require this token. Try to follow the tutorial properly and you will find where you have gone wrong.

Does the order of the routes effect which controller is accessed?

In my app I have grants and I want the url to be root/grant_id instead of root/grants/grant_id. I have this in my routes
Rails.application.routes.draw do
...
root 'static_pages#welcome'
# get 'home' => 'static_pages#home'
get 'about' => 'static_pages#about'
get 'faq' => 'static_pages#faq'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'dashboard' => 'dashboard#index'
resources :users do
resources :projects
member do
get 'access_granted'
put 'access_granted'
get 'remove_access'
put 'remove_access'
end
end
resources :profiles
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resource :request_access, only: [:show, :new, :create]
resources :grants, :path => '' do
resources :app_types do
resources :submissions
end
end
get 'grants' => 'grants#index'
resources :matches
end
When I put resources :matches below the resources :grants, :path => '' do line I get the error "Couldn't find Grant" and I see that request parameters are
{"controller"=>"grants", "action"=>"show", "id"=>"matches"}. When I put resources :matches above the the grant line everything works fine. Its almost like something in the grant route isn't closing and is forcing any lines below it to look for the grant controller. A simple solution is just keeping everything above that line but I'm trying to understand why this is happening.
I also noticed that even though I define the grant#index as grants, when I rake routes I see:
grants GET / grants#index
GET /grants(.:format) grants#index
So two questions
1. Is :path => '' the correct way to remove the grants/ part of the url.
2. Why is everything below the grants route getting sent to the grants controller?
From the documentation :
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
So, the problem you're having is that matching grants to "" means your grants INDEX route is /, and your grants SHOW route is /:grant_id, which will match any route. If you want to have this kind of route (which I would advise against), it has got to be at the bottom of the routes file.
You can read more about routing here: http://guides.rubyonrails.org/routing.html

How to to route "nested" paths by using `namespace` or `scope :module` / `scope :path`?

I am using Ruby on Rails 4.2 and I would like to route "nested" paths by using namespace or scope :module / scope :path within the block of a resource.
That is, I have the following route:
resources :users, :only => [:show]
that matches
user_path GET /users/:id(.:format) users#show
I would like to match the following paths
users_sessions_path POST /users/sessions users/sessions#create
user_session_path GET /users/:id/session users/sessions#show
delete_user_session_path GET /users/:id/session/delete users/sessions#delete
user_session_path DELETE /users/:id/session users/sessions#destroy
I read the official documentation and I tried to state something like
resources :users, :only => [:show] do
scope :module => :users do
scope :module => :sessions do
# scope :path => :sessions do
# namespace :sessions do
...
end
end
end
but no attempt has been successful. How should I state routes?
Update after the #dgilperez answer
I tried the following code
resources :users, :only => [:show] do
scope :module => :users do
resource :session, :only => [:show, :new, :create, :destroy] do
get :delete, :on => :collection, :to => 'sessions#delete'
end
end
end
that matches
delete_user_session_path GET /users/:user_id/session/delete(.:format) users/sessions#delete
new_user_session_path GET /users/:user_id/session/new(.:format) users/sessions#new
user_session_path POST /users/:user_id/session(.:format) users/sessions#create
user_session_path GET /users/:user_id/session(.:format) users/sessions#show
DELETE /users/:user_id/session(.:format) users/sessions#destroy
but I still need to map new and create actions without needing to pass the :user_id parameter. That is, I would like to map something like
new_user_session_path GET /users/session/new(.:format) users/sessions#new
user_session_path POST /users/session(.:format) users/sessions#create
I think you are overcomplicating it: you don't need to use scope or path to render nested resources. You just nest them:
resources :users, :only => [:show] do
resources :sessions
end
will render the following routes:
user_sessions GET /users/:user_id/sessions(.:format) sessions#index
POST /users/:user_id/sessions(.:format) sessions#create
new_user_session GET /users/:user_id/sessions/new(.:format) sessions#new
edit_user_session GET /users/:user_id/sessions/:id/edit(.:format) sessions#edit
user_session GET /users/:user_id/sessions/:id(.:format) sessions#show
PATCH /users/:user_id/sessions/:id(.:format) sessions#update
PUT /users/:user_id/sessions/:id(.:format) sessions#update
DELETE /users/:user_id/sessions/:id(.:format) sessions#destroy
user GET /users/:id(.:format) users#show
Those are not the routes you mention you need, but I'd like you to reconsider if you really need them like that, with namespaced controllers and custom names such as delete_user_ or you prefer to go more standard. If you really need those exact routes, let me know.
UPDATE after OP's update
To get the two routes missing you need to get them out the rested resource. I'd just write them directly like this:
resources :users, :only => [:show] do
scope :module => :users do
resource :session, :only => [:show, :destroy] do
get :delete, :on => :collection, :to => 'sessions#delete'
end
end
end
get 'users/session/new', to: 'users/sessions#new', as: :new_user_session
post 'users/session', to: 'users/sessions#create'

rails4 ckeditor (No route matches [POST] "/ckeditor/

I have installed Rails4 (rails (4.1.1))
I Installed Ckeditor+paperclip (http://rubydoc.info/gems/ckeditor/4.0.11/frames)
But if I try to upload images, I get this error in log:
ActionController::RoutingError (No route matches [POST] "/ckeditor/EDITOR.config.filebrowserImageUploadUrl"):
i need help please! This was worked In rails 4.0.0.
My route list
... mount Ckeditor::Engine => '/ckeditor'
I use Ckeditor with Paperclip
My config/routes.rb
Rails.application.routes.draw do
resources :binaries
resources :uploads
resources :coms
resources :comments
mount Ckeditor::Engine => '/ckeditor/'
resources :parts
get "/parts/page/:id" => "parts#page"
resources :answer_types
resources :from_sender_msgs
resources :ufknews
get "/general" => "ufknews#general"
get 'ufk13/ufk13pol'
get 'ufk13/governance'
get 'ufk13/contacts'
get "errors/error_404"
get "errors/error_403"
devise_for :users
devise_scope :user do
get "sign_in" => "devise/sessions#new"
get "logout" => "devise/sessions#destroy"
delete "logout" => "devise/sessions#destroy"
get "users" => "users#index"
get "users/:id" => "users#show" , as: :user_root, as: :user
get "users/:id/edit" => "users#edit" , as: :user_edit
get "users/delete" => "users#delete"
put "users" => "users#update"
end
get 'persons/profile'
resources :budget_types
resources :positions
resources :message_types
resources :message_states
resources :messages
resources :senders
resources :organisations
root 'ufknews#general'
get 'home/index'
get 'home/about'
get 'home/contacts'
get 'home/dufk'
get 'home/photo'
get "/*other" => redirect("/errors/error_404")
end
So, you can modify your routes to use POST request instead PUT while try to update data with ckeditor.
For example, if you have in your routes.rb:
resources :pages
you can update this to:
resources :pages, :except => ['update']
post 'pages/:id' => 'pages#update'
It's all!
This method kill a two rabbits:
Modify route for ckeditor;
Improve your project to update big fields over header size limits on your backend of client browser.

How to remove controller names from rails routes?

I would like to trim down the routes on my application so that:
http://myapplication.com/users/peter/questions/how-do-i-create-urls
becomes...
http://myapplication.com/peter/how-do-i-create-urls
I have a users controller and would like it to be resourceful. Users also have a nested resource called questions.
Basic routes file
Without any URL trimming, the routes file looks like this:
...
resources :users do
resources :questions
end
However the URLs from this take the form of
http://myapplication.com/users/peter/questions/how-do-i-create-urls
rather than
http://myapplication.com/peter/how-do-i-create-urls
Partial success
I have tried doing the following:
...
resources :users, :path => '' do
resources :questions
end
This works and produces:
http://myapplication.com/peter/questions/how-do-i-create-urls
However if I try:
...
resources :users, :path => '' do
resources :questions, :path => ''
end
Then things start to go wrong.
Is this the right approach and if so, can it be made to work with nested resources too?
The way you are doing it should work. I don't know what problem you are experiencing but if you copied the example code from your app directly then it might be because of the extra end that you have put in your routes. It should probably look like this:
resource :users, :path => '' do
resource :questions, :path => ''
end
Another thing that could be the cause and that you need to be vary careful about is that these routes pretty much catches all requests and you should have them last in your routes.rb so that other routes matches first. Take this scenario for example:
resource :users, :path => '' do
resource :questions, :path => ''
end
resources :posts
If you do it this way then no request will ever be routed to the Posts controller since a request to /posts/1 will be sent to the Questions controller with :user_id => 'posts', :id => 1
Edit:
Also, I now noticed that you use resource instead of resources. Don't know if that is intended or if it is a mistake.
Thanks to both #mark and #DanneManne for their help. With their input and a little more tweaking I got it all working. It's not exactly trivial but I'm not sure you could make it much shorter either:
Final working code
# setup the basic resources while holding some back for creation below
resources :users, :except => [:show, :index, :new, :create], :path => '/' do
resources :questions, :except => [:show]
end
# for clarity, pick out routes that would otherwise go
# to root (such as new_user => '/new')
resources :users, :only => [:index, :new, :create]
# setup questions#show to give clean URLS
match ':user_id/:question_id', :as => :user_question,
:via => :get,
:controller => :questions,
:action => :show
# setup users#show method to give clean URLS
match ':user_id', :as => :user,
:via => :get,
:controller => :user,
:action => :show
Rake Routes output
user_questions GET /:user_id/questions(.:format) {:action=>"index", :controller=>"questions"}
POST /:user_id/questions(.:format) {:action=>"create", :controller=>"questions"}
new_user_question GET /:user_id/questions/new(.:format) {:action=>"new", :controller=>"questions"}
edit_user_question GET /:user_id/questions/:id/edit(.:format) {:action=>"edit", :controller=>"questions"}
user_question PUT /:user_id/questions/:id(.:format) {:action=>"update", :controller=>"questions"}
DELETE /:user_id/questions/:id(.:format) {:action=>"destroy", :controller=>"questions"}
edit_user GET /:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user PUT /:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /:id(.:format) {:action=>"destroy", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
user_question GET /:user_id/:question_id(.:format) {:controller=>"questions", :action=>"show"}
user GET /:user_id(.:format) {:controller=>"user", :action=>"show"}
Not sure about the nesting but try
:path => '/'
Just thought I'd add another possible solution, in case anybody else arrives from Google at a later date.
After looking at this nested resource article, this could be a cleaner solution with almost the same flexibility in routes:
resources :users, :path => ''
resources :users, :path => '', :only => [] do
resources :questions, :path => '', :except => [:index]
Basically, by including the second parent block, the child resources aren't yielded before the parent resources.
This particular example also assumes that having complete routes for the parent block is more crucial than those for the child. Consequently, the child block is limited from having an index, but this might be acceptable, depending on the situation.

Resources