I installed Devise to get authentication into my application. I followed all the instructions about the gem and that part seemed to go well.
However now that I am trying to link to the Register page, I get a crazy error I can't figure out... For some reason, rails can't figure out where the devise controllers are located...
from: public/index.html.erb (public landing page)
<div class ="getstarted">
<table>
<tr>
<td id = "introtext">
<p>Foo is good..l here is why: </p>
<ul>
<li>Improved quality</li>
<li>Technology consistencies </li>
<li>Cost efficiencies</li>
<li>Increased security</li>
<li>Improved adherence to compliance standards</li>
</ul>
</td>
<td id = "buttons">
<%= button_to "Sign Up for an Account", new_user_registration_path, :method => "get" %>
<br/>
<%= button_to "Log In to your Account", new_user_session_path, :controller => "devise/session", :method => "get" %>
</td>
</tr>
</table>
</div>
routes.rb has the following line:
devise_for :users
and the Routes table seems to have what it needs:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
When I click on the button to register I go to the link:
http://localhost:3000/users/sign_up?
and Rails gives me the error:
No route matches {:controller=>"devise/public"}
Same thing for the log-in button:
http://localhost:3000/users/sign_in?
No route matches {:controller=>"devise/public"}
I set up a view for "public" to be my public viewable (pre authentication) pages... and I see that Rails is looking for that inside the devise directory (which doesn't exist).
I also tried deleting the ? at the end of the URL... that didn't produce any difference.
I'm stuck and a couple hours already invested. Hope someone can help me out.
For your "Log in to your account" button, I would change the current route from 'new_user_session_path,' to 'user_session_path'.
Also I would change 'button_to' into 'link_to'
See what that does
Related
I am learning ruby on rails by making a project web app,
whenever i go to <%= link_to 'My Profile', user_path(:id) %> error Couldn't find User with 'id'=id" is shown...and url is "http://localhost:3000/users/id
If i convert above link to <%= link_to 'My Profile', user_path %>, it works but now all other pages except user's show page give error No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]....I cant seem to find any solution anywhere...
Here are my configs:
rake routes Prefix Verb URI Pattern Controller#Action
root GET / static#landing
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user GET /users/:id(.:format) users#show
_header.html.erb
<%= link_to 'My Profile', user_path %>
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
root 'static#landing'
devise_for :users
resources :users, :only => [:show]
You need to pass the id of a user to user_path, not the symbol :id - so something like
<%= link_to 'My Profile', user_path(#current_user.id) %>
if the user you want to link to is in an instance variable - or since you are using Devise and it provides the current_user helper you might be wanting
<%= link_to 'My Profile', user_path(current_user.id) %>
I think the first thing wrong is this:
<%= link_to 'My Profile', user_path(:id) %>
You are using a symbol, not a variable
<%= link_to 'My Profile', user_path(#id) %>
Would have an actual variable, assuming you have set #id in your controller.
Normally however you would not set the id for a user in a path - it's a recipe for someone to just change the number manually and view someone elses.
Lets assume you set a session variable called :user_id, then you should do something adding a collection route - so no id is sent.
resources :users do
get :show_profile, on: :collection
end
This gets you a path to your show_profile action, called with
link_to 'My Profile', show_profile_user_path
Then in your controller do your user look up from the session - so something like:
#user = User.find_by id: session[:user_id]
I don't know how devises does things, but please don't trust a user to not fiddle with your parameters!
I'm trying to create a simple search form in my Rails application. I get an error with the url path of the form:
<%= form_tag(med_search, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_to "Search", class: "btn btn-default" %>
<% end %>
The first line above causes error undefined local variable or methodmed_search' for #<#:0x007fab2b5afa90>`
The problem is most likely with my routes setup. I created a new controller action called search so I edited my routes.db to look like this:
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
devise_for :users
#get 'meds/index'
root to: "meds#index"
resources :meds, :path => ''
end
When I do rake routes, I am seeing the path med search so I know the url is valid:
Prefix Verb URI Pattern Controller#Action
med_search GET /meds/:med_id/search(.:format) meds#search
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / meds#index
GET / meds#index
POST / meds#create
GET /new(.:format) meds#new
GET /:id/edit(.:format) meds#edit
GET /:id(.:format) meds#show
PATCH /:id(.:format) meds#update
PUT /:id(.:format) meds#update
DELETE /:id(.:format) meds#destroy
What should I change in the routes to fix this?
Thanks!
EDIT: changed url to med_search_path, get new error: No route matches {:action=>"search", :controller=>"meds"} missing required keys: [:med_id]. Looks like it's related to the route /meds/:med_id/search(.:format)
1) you need to change your form_tag like this
<%= form_tag(search_meds_path, :method => "get", id: "search-form") do %>
2) You need to change your route from member to collection like this
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
3) Not sure why you need to add resources :meds, :path => '' at the bottom again. Incase you dont need it, it is better to remove.
It looks like you're trying to code up a search but because you put it in the resources block Rails is assuming you're talking about a specific med.
Remove the route from the resources block and change it to get 'meds/search' => 'meds#search'. That will allow you to use it as just a regular endpoint without Rails complaining that you need an ID.
Devise destroy session and sign out from controller?
if something_is_not_kosher
# 1. log this event, 2. send notice
redirect_to destroy_user_session_path and return
end
Also tried:
if something_is_not_kosher
# 1. log this event, 2. send notice
redirect_to controller: 'devise/sessions', action: 'destroy', method: :delete and return
end
Error is No route matches [GET] "/users/sign_out" but I'm explicitly setting method: :delete in example 2. Maybe devise has a method? current_user.sign_out and tried sign_out(current_user) which also don't work? Thanks for the help.
rake routes:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
Why don't you just use devise's built-in sign_out_and_redirect(current_user) method?
So I ended up solving this by creating a custom signout route
devise_scope :user do
get '/signout', to: 'devise/sessions#destroy', as: :signout
end
and in my controller I have:
if something_is_not_kosher
redirect_to signout_path and return
end
destroy_user_session_path(#user) is sign out path for user, but it must requst with DELETE method. redirect_to method will tell broswer to request another path, but broswer just can request with GET method.
So if you want to let user to sign out, you must set a sign out form with DELETE method or with AJAX request to let user sign out but not with redirect_to function.
If you just want to destroy user session, use sign_out #user is ok.
Just in case someone can't use it directly.
<% if user_signed_in? %>
<li><%= link_to "Logout", destroy_user_session_path, :method => :delete %></li>
<% else %>
<li><%= link_to "Sign up now!", new_user_registration_path%></li>
<% end %>
Full disclosure, I'm a no00b. That said... I'm trying to use a bootstrap button that when clicked links to a devise registration page. I either get an error that I have an undefined method or it completely messes up my bootstrap format. Can someone please explain what is going on?
This line of code messes up the bootstrap formatting
<%= link_to "Sign Up", new_user_registration_path, :class => "btn btn-success" %>
Edit: rake routes path shows I have new_user_registration_path
new_user_session GET /auth/login(.:format) devise/sessions#new
user_session POST /auth/login(.:format) devise/sessions#create
destroy_user_session DELETE /auth/logout(.:format) devise/sessions#destroy
user_password POST /auth/secret(.:format) devise/passwords#create
new_user_password GET /auth/secret/new(.:format) devise/passwords#new
edit_user_password GET /auth/secret/edit(.:format) devise/passwords#edit
PATCH /auth/secret(.:format) devise/passwords#update
PUT /auth/secret(.:format) devise/passwords#update
cancel_user_registration GET /auth/register/cancel(.:format) devise/registrations#cancel
user_registration POST /auth/register(.:format) devise/registrations#create
new_user_registration GET /auth/register/cmon_let_me_in(.:format) devise/registrations#new
edit_user_registration GET /auth/register/edit(.:format) devise/registrations#edit
PATCH /auth/register(.:format) devise/registrations#update
PUT /auth/register(.:format) devise/registrations#update
DELETE /auth/register(.:format) devise/registrations#destroy
This error has been driving me and a fellow dev insane. We're building an app with Ruby/Rails jazz, and whenever I click to logout for a user session, I get this error:
Routing Error
No route matches [GET] "/users/sign_out"
Try running rake routes for more information on available routes.
Now I've ran rake routes a ton, and I get:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / home#index
about /about(.:format) home#about
Is there anything to be done here? I've also followed the answers of a lot of the posts here on Stack Overflow to no avail. Anything else I can test to try to fix this problem?
EDIT: Here is logout link code
<a href="/users/sign_out" class="header-links right-link" data-method="delete" rel="nofollow">Logout</a
Inspect you HTML and make sure rails.js is loaded, and there are no javascript errors. And if you are using jQuery, make sure that there is noConflict.
Note: My guess is you are running rails version < 3.1, So check if these two lines are present in your layout
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
Try:
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
does your routes have this under "devise_for"? :
get 'users/sign_out' => 'sessions#destroy', :as => :destroy_user_session
And try this as link:
<%= link_to "Logout", destroy_user_session_path, method => :delete %>
Check to see if your routes.rb has,
devise_for :users, ActiveAdmin::Devise.config
Sometimes my installs seem to miss this part ActiveAdmin::Devise.configand then I get this error.
I see it in the gem's generator but sometimes it doesn't get added on my apps.