Rails[Devise]: devise breaking navbar links - ruby-on-rails

I have a Bootstrap navbar, but the tabs are generated at runtime. It's this way because they link to show/:id and a user can delete the record associated with that tab at any time. I created the links like this:
<% #groups.each do |group| %>
<li id=<%= group.id %>><%= link_to t("navbar." + group.name.singularize.downcase), :controller => 'groups', :action => 'show', :id => group.id %></li>
<% end %>
This works fine until I use Devise and attempt to get to /admins/sign_in. The navbar code is still exactly the same, but I get an UrlGenerationError:
No route matches {:action=>"show", :controller=>"devise/groups", :id=>4, :locale=>nil}
My guess is the error stems from the controller being "devise/groups" as that's the only difference I spot. Is there a way I can tell it to not prepend "devise"? Or do I have to write new routes for all these bits? If I have to add new routes, how can I use resources in routes?
I suspect Devise will also break other links on other pages that I had to code this way.
routes:
Rails.application.routes.draw do
devise_for :admins
get 'search/index'
get 'tags/:tag', to: "search#index", as: :tag
scope "(:locale)", :locale => /#{I18n.available_locales.join("|")}/ do
get 'home/index'
root :to => "home#index"
resources :brands
resources :faqs
resources :categories
resources :subgroups
resources :groups
end
UPDATE: I tried changing the link to the following
<% #groups.each do |group| %>
<li id=<%= group.id %>><%= link_to(t("navbar." + group.name.singularize.downcase), url_for(:controller => 'groups', :action => 'show', :id => group.id)) %></li>
<% end %>
But it still comes up with "devise/groups" as the controller when I access the sign-in page. After I sign in, there's no problem.

I have a strange "fix" but it certainly does not feel correct in the slightest. I created new Controllers that derive from Devise::[name]Controllers, and did the following to whatever methods I needed
def new
super
end
Then edited routes.rb
devise_for :admins, :controllers => {:[name] => "[name]"}
with a new :[name] => "[name]" pair for each Controller I had to make.
determined which ones I needed by looking at rake routes. Everything's working, but I'm sure there's a cleaner fix than this out there.
p.s. you may need to run rails g devise:views

Related

form_for routing error with scope :module nested routes

This may not make sense but I'm trying to learn harder stuff and progress, it seems like I'm missing the ID for address but can't seem to find a solution.
I included the url in form_for because when I remove it, the app breaks. But seems like I predefined the url than edit breaks.
<%= form_for([#address.user, #address], :url => user_addresses_path) do |f| %>
Error Readout:
No route matches [PATCH] "/users/1/addresses"
When I remove :url=>
undefined method `user_client_address_path'
routes.rb
Rails.application.routes.draw do
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
rake routes partial output (let me know if more is needed)
user_addresses GET /users/:user_id/addresses(.:format) client/addresses#index
POST /users/:user_id/addresses(.:format) client/addresses#create
new_user_address GET /users/:user_id/addresses/new(.:format) client/addresses#new
edit_user_address GET /users/:user_id/addresses/:id/edit(.:format) client/addresses#edit
user_address GET /users/:user_id/addresses/:id(.:format) client/addresses#show
PATCH /users/:user_id/addresses/:id(.:format) client/addresses#update
PUT /users/:user_id/addresses/:id(.:format) client/addresses#update
DELETE /users/:user_id/addresses/:id(.:format) client/addresses#destroy
If you are using AJAX to submit the form, try adding method: 'POST' to it.
If you are submitting the form normally, try adding method: :post to the form_for hash.
It should end up something like:
<%= form_for([#address.user, #address], url: user_addresses_path, method: :post) do |f| %>

Couldn't find User with id=sign_out

Sign-out link isn't working in my rails application.
I have checked my routes.rb which is listed below and my application.html.erb looks to follow the right path.
Getting the following error.
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Rails.root: /Users/patrickwalsh/rails_projects/ytutorial
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:4:in `show'
lib/disable_assets_logger.rb:11:in `call'
My routes.rb
Refectory::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "users" }
devise_scope :user do
get 'login', to: "devise/sessions#new", as: "login"
get 'logout', to: "devise/sessions#destroy", as: "logout"
get 'logout', to: "users/sessions#destroy", as: "logout"
get 'signup', to: "users#new", as: "signup"
match '/users/:id', :to => 'users#show', :as => :user
end
root :to => 'tutorials#index'
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
get 'users/:id' => 'users#show'
end
resources :tutorials
resources :comments, only: [:show, :create, :update, :destroy]
resources :tutorials do
member { post :vote }
end
if Rails.env == "development"
match 'errors/404' => 'errors#error_404'
match 'errors/500' => 'errors#error_500'
end
unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end
match 'tagged' => 'tutorials#tagged', :as => 'tagged'
end
and my application.html which seems to be following the right route from what I can see.
Any help is greatly appreciated!
<% if current_user.present? %>
<li><%= link_to "Log out", destroy_user_session_path, (:method => "delete") %></li>
<% else %>
<li><%= link_to "Log in", new_user_session_path %></li>
<li><%= link_to "Sign up", new_user_registration_path %></li>
<% end %>
My users controller as well as I have a suspicion this is where the problem lies but not sure what the error is.
class UsersController < Devise::RegistrationsController
def show
#user = User.find(params[:id])
#tutorials = #user.tutorials
end
end
I had the same issue. My routes were in the correct order, the link_to method was properly used, and Rails kept triggering the users/:id route with :id => :sign_out. I figured out that was because I removed jquery_ujs from my application.js file...
jquery_ujs handles the data-method attribute in the links (generated by link_to when you use the method option), which is used to determine the correct route as explained here: https://thoughtbot.com/blog/a-tour-of-rails-jquery-ujs
So just make sure the you have the following included in your application.js:
//= require jquery
//= require jquery_ujs
If you are calling /users/sign_out directly from the URL it won't work because the routes is:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
id est, it uses the DELETE method. You need to change your devise initializer to:
config.sign_out_via = :get
other option would be to create a small form and its button with DELETE as method.
This worked for me
#form
<%= link_to(destroy_user_session_path, {:class => "nav-link", :method => :delete}) do %>
<span class="sidebar-normal"> Logout </span>
<% end %>
#routes
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
I have started noticing this error after removing rails-ujs. This was done as part of the upgrade to Rails 7 with ImportMap and Hotwire suite. Changing link_to to button_to has fixed this error in this case.
<%= button_to 'Log out', destroy_user_session_path, method: :delete %>
https://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#replacements-for-rails-ujs-functionality
None of this solutions worked for me.
Also it happens just in development mode for me...
I fixed it by adding
if params[:id] = "sign_out"
sign_out current_user
return
end
in the set user function. Not the prettiest solution but it works...
You need to move:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
over your devise_scope. Rails is looking for routes from top of Routes file. Your sign out url matches users/:id, hence it is trying to render show action with sign_out being an id.
UPDATE:
Actually, do you really need the last line in your devise_scope block?
Since non of the other answers worked, I found that you could change the base path for every Devise endpoint as described here. So, what I did was to user devise_for on routes.rb:
devise_for :users,
path: 'devise'
Then, all my Devise routes started with devise instead of users so the conflict was gone.
Sorry to bump this up but the "correct" anwser is not to mess around with routes risking breaking many things.
IF you want a disconnect on a GET then actually configure Devise like so in initializer/devise.rb
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
As mentionned in the Devise documentation.
Short answer: use link_to with method DELETE
<%= link_to 'Logout', destroy_user_session_path(current_user), method: :delete %>
explanation:
If you take a look at your Routes you'll see
Helper
destroy_user_session_path
the HTTP verb is
DELETE
the Path
/users/sign_out(.:format)
Controller#Action
devise/sessions#destroy
link_to defaoult method is get, this is "the why" (in a simplistic way of explaning). However, you can specify by using method: :method name
I got this error when using devise in a rails 7 app (with bootstrap):
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=sign_out)
I don't fully understand it myself yet, but basically just open another terminal window, type ./bin/dev and hit enter, and (I guess) it loads your javascripts.

How to add new view to Ruby on Rails Spree commerce app?

A very basic question that I cannot seem to solve is how to add a new view to my Ruby on Rails Spree commerce application. What I want to do is have a link next to the Home link in the _main_nav_bar.html.erb and when you click it have displayed an about page at the place where the products are displayed. So:
home about cart
---------------------
things of the HOME page
---------------------
footer
Click on about leads to:
home about cart
---------------------
things of the ABOUT page
---------------------
footer
In views/shared/_main_nav_bar.html.erb the link I created (based on the home link) looks as follows:
<li id="home-link" data-hook><%= link_to Spree.t(:home), spree.root_path %></li>
<li id="about-link" data-hook><%= link_to Spree.t(:about), spree.about %></li>
The AboutController I created looks as follows:
module Spree
class AboutController < Spree::StoreController
def index
end
end
end
And finally, in config/routes.rb I added the following code:
root :about => 'about#index'
When I now try to start the server it just does not work anymore without giving an error message.
Can someone please help me out on this issue? How do I add a view and create a working link that loads in the main div?
EXTRA: routes.rb
MyStore::Application.routes.draw do
mount Spree::Core::Engine, :at => '/'
Spree::Core::Engine.routes.prepend do
#get 'user/spree_user/logout', :to => "spree/user_sessions#destroy"
end
get '/about' => 'spree/about#index'
get '/contact' => 'spree/contact#index'
end
You need to do in routes.rb:
Spree::Core::Engine.routes.prepend do
get '/about', :to => 'about#index', :as => :about
end
or without the Spree::Core scope:
get '/about', :to => 'spree/about#index', :as => :about
Because, you have your about_controller.rb i.e. AboutController defined inside Spree module. And, hence you'll have to reference the spree namespace in your route to set it properly.
In your views:
<li id="about-link" data-hook><%= link_to Spree.t(:about), spree.about_path %></li>
or
<li id="about-link" data-hook><%= link_to Spree.t(:about), main_app.about_path %></li>

Rails 3 Routing: non-CRUD controller

I need a bit of help with converting routing from Rails 2 to Rails 3.
In app/views/layouts/application.html.erb, I have:
<%= link_to "Reports", reports_path %><br>
There is a ReportsController, and in app/views/reports/index.html.erb, I have this:
<%= link_to "Clients With Animals", :action => "getAnimals", :controller => "clients" %>
Then, in config/routes.rb, I have this (Rails 3)
match '/reports' => "reports#index"
match '/clients/getAnimals', to: "clients#getAnimals"
I get this error when I click on the "getAnimals" link on the reports page:
ActiveRecord::RecordNotFound in ClientsController#show
Couldn't find Client with id=getAnimals
I don't want "getAnimals" to be the ID - I want it to be the action, instead.
How do I do that?
Assuming you also have a resources :clients entry, you want to make sure match '/clients/getAnimals', to: "clients#getAnimals" is above it (Rails will match whatever it hits first).
However, the better way may be to put it in the resource:
resources :clients do
get 'getAnimals', :on => :collection
end

trouble getting route to work in Rails

I have the following in my routes.rb:
resources :resources
match "/resources/:category" => 'resources#index', :as => :resources
In my index.html.erb I have the following:L
<% #categories.each do |category| %>
<li class="active">
<%= link_to category.name, resour_path(:category=>category.name.parameterize), :class => "large", remote => true %>
</li>
<% end %>
I want to submit to the index action of the ResourcesController because I want to show different items on the page as in filter.
The links currently are giving an error as they are calling the show action and not going to the index action.
How can I get them to submit to the index action of the routes controller?
The line match "/resources/:category" is never reached when going for the route. The path /resources/some-category is matched by resources :resources, and some-category is considered the id for the show action.
Option 1
Switch the lines so the match line is matched first.
match "/resources/:category" => 'resources#index', :as => :resources
resources :resources
Option 2
Turn
resources :resources
into
resources :resources, :except => :show
References
As described in http://guides.rubyonrails.org/routing.html:
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.

Resources