I have an application where a user can make microposts. But enter one into the form and press post, it says No route matches [POST] "/users/1". Here is my current routes.rb file .
SampleApp::Application.routes.draw do
resources :users do
resources :comments
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy] do
resources :comments
end
resources :relationships, only: [:create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
# The priority is based upon order of creation:
and this is the output when I run rake routes:
user_comments GET /users/:user_id/comments(.:format) comments#index
POST /users/:user_id/comments(.:format) comments#create
new_user_comment GET /users/:user_id/comments/new(.:format) comments#new
edit_user_comment GET /users/:user_id/comments/:id/edit(.:format) comments#edit
user_comment GET /users/:user_id/comments/:id(.:format) comments#show
PUT /users/:user_id/comments/:id(.:format) comments#update
DELETE /users/:user_id/comments/:id(.:format) comments#destroy
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
micropost_comments GET /microposts/:micropost_id/comments(.:format) comments#index
POST /microposts/:micropost_id/comments(.:format) comments#create
new_micropost_comment GET /microposts/:micropost_id/comments/new(.:format) comments#new
edit_micropost_comment GET /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit
micropost_comment GET /microposts/:micropost_id/comments/:id(.:format) comments#show
PUT /microposts/:micropost_id/comments/:id(.:format) comments#update
DELETE /microposts/:micropost_id/comments/:id(.:format) comments#destroy
microposts POST /microposts(.:format) microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
relationships POST /relationships(.:format) relationships#create
relationship DELETE /relationships/:id(.:format) relationships#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
I guess what I really need is for [POST] users/:id to route to microposts#create, but I don't know the syntax for it.
p.s. If I add post post "users/:id", :controller => "users/update", I get the error:
ArgumentError (missing :action):
config/routes.rb:4:in `block (2 levels) in <top (required)>'
config/routes.rb:2:in `block in <top (required)>'
config/routes.rb:1:in `<top (required)>'
here is the form which is talking to the controller (i think)
<%= form_for :micropost do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
In your form_for you just need to specify url. I think this should work.
<%= form_for :micropost, :html => {:method => :post, :url => microposts_path} do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
Also let me know what is html output. In form_for :html parameter is given as a hashmap, where key are form attributes and values are attribute values.
Thanks
I'm new to rails 3 (and rails in general)...I built a site skeleton for a friends band and now he wants to add articles to his site...
So far all I had built were pages: (Home, Shows, Media, Contact) and header & footer partials... so nothing too fancy yet.
Heres what I did to add articles so far:
rails g scaffold article title:string body:text
rake db:migrate
but when I went to localhost:3000/articles I get this error message:
ActionController::RoutingError in Articles#new
no route matches {:action=>"home", :controller=>"articles"}
it says the error was raised in app/views/layouts/_header.html.erb on line #28 :
25: <h1>Title</h1>
26: <ul id="nav">
27: <ul>
28: <li><%= link_to image_tag("home.jpg",:class=> 'hoverImages'), :action => 'home' %></li>
29: <li><%= link_to image_tag("shows.jpg", :class=> 'hoverImages'), :action => 'shows' %></li>
30: <li><%= link_to image_tag("media.jpg", :class=> 'hoverImages'), :action => 'media' %></li>
31: <li><%= link_to image_tag("contact.jpg", :class=> 'hoverImages'), :action => 'contact' %></li>
here is my routes.rb
CsmlSite::Application.routes.draw do
resources :articles
match '/shows', :to => 'pages#shows'
match '/media', :to => 'pages#media'
match '/contact', :to => 'pages#contact'
match '/articles', :to => 'articles#index'
root :to => "pages#home"
end
why can't i view localhost:3000/articles ?
any helpful hints would be MUCH obliged!
EDIT: here is the output of my rake route task
root /(.:format) {:controller=>"pages", :action=> "home"}
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format) {:action=>"new", :controller=>" articles"}
edit_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
shows /shows(.:format) {:controller=>"pages", :action=>"shows"}
media /media(.:format) {:controller=>"pages", :action=>"media"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
contacts POST /contacts(.:format) {:action=>"create", :controller=>"contact_us/contacts"}
new_contact GET /contacts/new(.:format) {:action=>"new", :controller=>"contact_us/contacts"}
contact_us /contact_us(.:format) {:action=>"new", :controller=>"contact_us/contacts"}`
Rewrite your view using paths instead of :action:
<h1>Title</h1>
<ul id="nav">
<li><%= link_to image_tag("home.jpg",:class=> 'hoverImages'), root_path %></li>
<li><%= link_to image_tag("shows.jpg", :class=> 'hoverImages'), shows_path %></li>
<li><%= link_to image_tag("media.jpg", :class=> 'hoverImages'), media_path %></li>
<li><%= link_to image_tag("contact.jpg", :class=> 'hoverImages'), contact_path %></li>
</ul>
And one more thing in your routes:
CsmlSite::Application.routes.draw do
root :to => "pages#home"
resources :articles
match '/shows', :to => 'pages#shows'
match '/media', :to => 'pages#media'
match '/contact', :to => 'pages#contact'
# the route below is not necessary (it is generated by `resources :articles`)
# match '/articles', :to => 'articles#index'
end
Nvm... figured it out. My routes were getting conflicted with a gems that I had installed... specifically gem 'contact_us', '~> 0.2.0'.
Once I removed that gem I was able to use shows_pages_path et. all
When I go to the path: /genre/new in my application I get this error:
myapp/app/views/genre/_form.html.erb where line #1 raised:
undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080>
However when I go to /genre/:id/edit the _form.html.erb file renders without error and the record is updated with no problems.
My new.html.erb and edit.html.erb files call <%= render 'form' %> and my _form.html.erb file has:
<%= form_for(#genre) do |f| %>
<%= f.label :title %> <br /> <%= f.text_field :title %>
<%= f.label :desc %> <br /> <%= f.text_field :desc %>
<%= f.submit %>
<% end %>
In genre_controller.rb my 'new' and 'edit' actions are as follows:
def new
#genre = Genre.new
current_user.authorize! :create, #genre # cancan authorization
respond_to do |format|
format.html # new.html.erb
format.json { render json: #genre }
end
end
def edit
#genre = Genre.find(params[:id])
current_user.authorize! :update, #genre # cancan authorization
end
I've run a search in my codebase for the string "genres" and the only place it occurs is in the logs, so I'm sure this is not a typo in my code.
My guess is that Rails routing system correctly pluralizes "genre" to "genre", but form_for (or a dependency) is creating the pluralization "genres", but only when the parameter passed to it is empty or "new".
Given the error is around 'genres_path', I tried various combinations of the following in my routes.rb file, but they didn't solve the problem:
match "/genres" => "genre#index", :as => :genre
match "/genres/:id(.:format)" => "genre#show", :as => :genre
match "/genre" => "genre#index", :as => :genres
match "/genre/:id(.:format)" => "genre#show", :as => :genres
Any thoughts on how I can work around this?
EDIT: Here are the routes generated by the resources :genre statement in my routes.rb file:
genre_index GET /genre(.:format) {:action=>"index", :controller=>"genre"}
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
new_genre GET /genre/new(.:format) {:action=>"new", :controller=>"genre"}
edit_genre GET /genre/:id/edit(.:format) {:action=>"edit", :controller=>"genre"}
genre GET /genre/:id(.:format) {:action=>"show", :controller=>"genre"}
PUT /genre/:id(.:format) {:action=>"update", :controller=>"genre"}
DELETE /genre/:id(.:format) {:action=>"destroy", :controller=>"genre"}
on new.html.erb try
<%= form_for(#genre, :url => genre_path, :method => :post) do |f| %>
assuming you have your route setup as a resource - resources :genre
also this will not work on edit.html.erb
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Update:
this is the one we are interested in
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
try this
<%= form_for(#genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>
Here is my actual error: No route matches [GET] "/members/sign_out"
Since most people will use "users" I thought it would be more helpful to have that in the title. At any rate, I am essential unable to logout. I can successfully edit my member profile.
I am using devise 1.4.2 and Rails 3.1.0.rc4. Also, I have generated two separate devise models - one called "members" and the other called "admins". I was able to register and log into both of them (simultaneously) by manually navigating to the correct URL path (i.e., localhost:3000/admins/sign_in/). I created some links within my application.html.haml layout file by following this RailsCast on Devise. I am aware that it only addresses signin/signout links for "members."
If I click on the signout link I get the above error. This occurs if I manually navigate to either signout URL (i.e., localhost:3000/admins/sign_out/).
Can someone tell me why this is happening? Below are the various related files. And of course, I'm a newbie...
rake routes output:
j(film_repo)$ rake routes
new_member_session GET /members/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
member_session POST /members/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
member_password POST /members/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_member_password GET /members/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_member_password GET /members/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /members/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_member_registration GET /members/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
member_registration POST /members(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_member_registration GET /members/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_member_registration GET /members/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /members(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /members(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
new_admin_session GET /admins/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
admin_session POST /admins/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_admin_session DELETE /admins/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
admin_password POST /admins/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_admin_password GET /admins/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_admin_password GET /admins/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /admins/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_admin_registration GET /admins/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
admin_registration POST /admins(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_admin_registration GET /admins/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_admin_registration GET /admins/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /admins(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /admins(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
films GET /films(.:format) {:action=>"index", :controller=>"films"}
POST /films(.:format) {:action=>"create", :controller=>"films"}
new_film GET /films/new(.:format) {:action=>"new", :controller=>"films"}
edit_film GET /films/:id/edit(.:format) {:action=>"edit", :controller=>"films"}
film GET /films/:id(.:format) {:action=>"show", :controller=>"films"}
PUT /films/:id(.:format) {:action=>"update", :controller=>"films"}
DELETE /films/:id(.:format) {:action=>"destroy", :controller=>"films"}
root / {:controller=>"films", :action=>"index"}
routes.rb
FilmRepo::Application.routes.draw do
devise_for :members
devise_for :admins
resources :films
root :to => 'films#index'
end
admin.rb (model)
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
member.rb (model)
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
application.html.haml
!!!
%html
%head
%title Film Repo
= stylesheet_link_tag 'compiled/screen.css', :media => 'screen, projection'
= stylesheet_link_tag 'compiled/print.css', :media => 'print'
/[if lt IE 8]
= stylesheet_link_tag 'compiled/ie.css', :media => 'screen, projection'
= csrf_meta_tag
%body.bp
#container
#user_nav
- if member_signed_in?
Signed in as #{current_member.email}. Not you?
\#{link_to "Sign out", destroy_member_session_path}
- else
= link_to "Sign up", new_member_registration_path
or #{link_to "sign in", new_member_session_path}
- flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}"
= yield
You can end a session via get by changing the devise configuration in initializers.
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
Just open the link and your session is removed.
I had a similar problem, but addition of the :method=> :delete didn't work.
I was able to add a new route for a the get request by commenting out the devise_for :users and adding
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end
I had a similar problem.
My view code was like this:
<%= link_to " exit", destroy_user_session_path, method: :delete %>
After adding the following change to routes.rb it worked,
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
Although I don't know the cause, the reason why you are getting that message is because in your routes you have
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
Which means that route is only available with the DELETE method as opposed to GET. This is a bit weird since in the docs for devise it says that it should create it as GET route (https://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb#L30)
With it as a DELETE route, you should be able to logout using
link_to :logout, destroy_member_session_path, :method => :delete
I just needed to add the
//= require jquery
//= require jquery_ujs
to my application.js
If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like:
button_to "Log out", destroy_user_session_path, method: :delete
if using link_to you must be sure to have javascript active:
Note that if the user has JavaScript disabled, the request will fall
back to using GET.
As seen in docs
In devise.rb, change
config.sign_out_via = :delete
to
config.sign_out_via = :get
This worked for me. I went crazy with this because the default is delete and I don’t understand why.
This works, but I am not sure whether it affects other elements in the application.
We still can use :method => :delete in my code, like that
= link_to "Sign out", destroy_user_session_path,:method => :delete
The reason i think we fail to load javascript that include jquery, make sure
= javascript_include_tag "application" (haml- you can use html too)
to include jquery-ui and jquery-ujs. So if it still error, i suggest to change rails gem in GEMFILE to version 3.2.6 and call bundle update to update gems. It works for me!
= link_to "Sign out", destroy_user_session_path,:method => :delete
will NOT work instead use this,
= link_to "Sign out", destroy_user_session_path,:method => 'delete'
should do the trick or worse case add require jquery_ujs in your application.js
In Rails 6:
I just changed the link_to to button_to, and the 'sign out' works properly
<%= button_to "Sign out", destroy_user_session_path, method: :delete %>
In rails 7, you need to add data: { turbo_method: :delete" } to link_to. So the link_to would look like this
<%= link_to "Log out", destroy_user_session_path, data: { turbo_method: :delete } %>
Using Rails4, I had to use the following method:
<%= link_to "Logout", destroy_admin_session_path, method: :delete %>
Emphasis on where the colons are on method: and :delete
Maybe that will help somebody.
Upgraded from Rails 3.0 to 3.1 and found this problem.
This fixed it for me:
routes.rb:
devise_for: users
devise.rb:
config.sign_out_via = :delete
application.html.erb:
<%= javascript_include_tag "application" %>
* not :defaults
_login_items.html.erb:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
app/assets/javascripts/application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
and I had in javascript/ jquery.js, jquery_ujs.js from 3.0 version that I've removed.
You may have removed assets/javascripts/*
Run rails generate jquery:install --ui this will generate all the javascripts as shown below
xxxx#xxxxx:~/Projects/Rails_apps/rtest$ rails generate jquery:install --ui
remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.7.1)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.16)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (822920)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js
Go to your layout e.g application.html.erb and edit <%= javascript_include_tag :all %>
That worked for me :)
FWIW I have also run into this problem. Have looked into all of the suggested answers however the only one which worked was to foto open routes.rb and comment out the following line:
devise_for :users
Below that, add the following line:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
The problem begins with rails 3.1 in assets/javascript/. Just look for application.js, and if the file doesn't exist, create a file with that name. I don't know why my file disappears or never was created on rails new app... that file is the instance for jquery.
#creamhost say,
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
but it is not correct solution for me (Rails4). I solved our problem (#Olives' answer),
link_to :logout, destroy_member_session_path, method: :delete
I am using rails version 5. I encounter this problem also. The simple fix I did was to changing the devise configuration in initializes to the default.
From :
config.sign_out_via = :get
To :
config.sign_out_via = :delete
Keep your devise.rb using the correct HTTP method:
# good
config.sign_out_via = :delete
# bad
config.sign_out_via = :get
Use button_to instead of link_to
# good
= button_to "Sign Out", destroy_user_session_path, method: :delete
# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"
If you are using bootstrap (keep it classy)
= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"
Ref: github.com/heartcombo/devise/issues/4570#issuecomment-740812109
Just use the following for your sign out link:
<%= link_to "Sign out", destroy_user_session_path, method: :delete %>
//= require jquery_ujs
You are missing this line in your assets. There's no need to get /users/signout request. Put this line into JavaScript file at very top of the page.
Had the same problem and remembered it only started happening after I decided to "clean up" my Javascript files. So I ran rails generate jquery:install --ui again and this solved it for me. (You can ignore the --ui part if you don't need JQuery UI, I suppose.)
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
That works for me
I believe this error is due to devise library template not aligned with changes on rails 7,
In short, you are not sending the request in method delete as expected due to the Javascript library that does that is now missing.
you can change :
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
with
<%= link_to "Sign out", destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
And it will work again
This worked for me:
<%= link_to 'Sign out', destroy_user_session_path, data: { turbo_method: :delete } %>
It happens only on windows.. Add the following thing to your Application.html.erb file.
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end