Devise route issue - ruby-on-rails

I am using devise for authentication on my project. I created a basic controller called panel with rails. When I am at the home url the link works as it should. But for some reason the link_to in my layout file attempts to find a route that does not exist when I go to /users/sign_in, the default login for devise.
In my layout file I have this link that should always be shown.
<%= link_to "Panel", :controller => "panel", :action => "index" %>
when I attempt to access the default user login path on devise /users/sign_in it gives the error: ActionController::RoutingError in Devise/sessions#new
No route matches {:controller=>"devise/panel"} from the layout file.
routes:
get "panel/index"
get "home/index"
devise_for :users
It looks like the routes go into some sort of devise scope when I click on the link for the users/sign_in path.

Try this:
routes.rb:
get "panel/index" => 'panel#index', :as => 'panel'
In your controller:
<%= link_to "Panel", panel_path %>

get "panel/index", :as => :panel_index
<%= link_to "Panel", panel_index_path %> |

Doing the following also solves it,
<%= link_to "Panel", :controller => "/panel", :action => "index" %>
Source: https://github.com/plataformatec/devise/issues/471

Related

high_voltage double URL routing error

I'm getting the following error when I routing in my app
Routing Error
No such page: pages/contact
The page does exist, I've noticed in the URL it says:
http://localhost:3000/pages/pages/contact So rendering double /pages/
Routes.rb
Rails.application.routes.draw do
get 'pages/index' => 'high_voltage/pages#show', id: 'index'
root :to => 'high_voltage/pages#show', id: 'index'
end
html.erb
<%= link_to 'pages/contact', :class => 'btn btn--sm btn--primary type--uppercase' do %>
It's important to note if I'm routing from index -> contact it's fine, however, if I route from another internal page such as about or team, it will double render in the URL
TIA
I was using the default rails routing system for High_voltage
Default rails is:
<%= link_to 'Privacy Policy', 'pages/about' %>
When the proper routing for high_voltage is:
<%= link_to 'About', page_path('about') %>
If i was already on localhost:3000/pages/about/ it'd try and render pages again as specified in the default rails link_to versus the high_voltage

Rails 5 link_to specific controller action

I have following routing problem in Rails 5:
<%= link_to product.id, product %>
generates a link like this
localhost:3000/products/12345
What I want is a link to the "ext" action in the products controller:
localhost:3000/products/ext/12345
If I try to build a link like this
<%= link_to 'To the product', :controller => :products, :action => :ext %>
it gives back following error:
No route matches {:action=>"ext", :controller=>"products"}
In the routes.rb I have
get "products/ext/:id", to: "products#ext"
Thanks for help!
Modify your routes to
get "products/ext/:id", to: "products#ext", as: :products_ext
and change your view to
<%= link_to products_ext_path(product) %>

Rails link changes another link

I have a Rails 4.1.6 application and use Devise for the 'user' model. The link to my sign in page works fine:
<%= link_to 'Sign in'.upcase, new_user_session_path %>
results in
SIGN IN
However, adding a link to an action on another controller breaks the link to sign_in:
<%= link_to( 'GET STARTED', {controller: :contacts, action: :new}, method: :get) %>
<%= link_to 'Sign in'.upcase, new_user_session_path %>
still produces the same HTML:
<a data-method="get" href="/contacts/new">GET STARTED</a>
SIGN IN
But, the 'SIGN IN' link no longer works, with this error:
ActionController::UrlGenerationError at /users/sign_in
No route matches {:action=>"new", :controller=>"devise/contacts"}
This answer is for a similar situation and tells me that 'Rails is looking for a partial scoped under Devise since this is the one under which you're rendering.' But, my scope understanding is weak and I'm still struggling with how to fix this.
# relevant portion of routes.rb:
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users
match '/contacts/new', to: 'contacts#new', via: 'get'
I'm not 100% sure why this is happening, but I would suggest dropping method: :get from your link. I'd also suggest using the contacts_new_path helper instead of using { controller: ..., action: ... }. Finally, you can define your route using the more standard resource :contacts, only: :new (which will change the method to new_contact_path).
So, routes.rb
resource :contacts, only: :new
In your_template.html.erb
<%= link_to 'GET STARTED', new_contact_path %>
<%= link_to 'Sign in'.upcase, new_user_session_path %>

link_to controller action in view

inside my routes.rb I have resources: x
but I also have another controller y, and in one of the pages I want to link to a 'new' action in controller x.
usually If I have a match statement defined in routers like
match 'signin', to: 'session#new'
I can go
<%= link_to "text", signin_path %>
but what do I do when I use resources as with controller x and need to link to the new action, without having to write match statements out in routes.rb
Thanks
<%= link_to 'New', new_controller_name_path %>
And you can view all your routes by type rake routes in your app directory.
And if you want change default path you can write in config/routes.rb smth like this:
match 'controller_name/new', :to => 'controller_name#new', :as => 'only_my_new', :via => :get
And then create link:
<%= link_to 'New', only_my_new_path %>
Try this
match '/signin' => 'session#new', :as => :new_session
now, your signin path is new_session_path
write in your view file
<%= link_to "SignIn", new_session_path %>
write in your terminal - $ rake routes
you can view your resources and other routes. for that choose your '/signin' path and write that path in your link.

Calling a controller's action without a named route in Rails 3

In Rails 3, is there a way to link to a controller's action using ajax without having a named route?
I tried <%= link_to 'Reload', '#', url_for(:action => :reload, :id => #user.id), :remote => true, :method => 'post' %>
but it returns with the error No route matches {:controller=>"users", :id=>2, :action=>"reload"}
My main concern is that I don't want the action to be called by someone typing in a route in the address bar. Is there another way to go about this?
Thanks!
Tim
if your User resource is in your routes.rb file then you need to add a route to the 7 restful actions.
resources :user, :member => {:reload => :get}
That will give you the route to work with
<%= link_to "Reload", user_reload_path(current_user)%>
and that should work for reloading your user object
rake routes
that will show you all your routes you can work with

Resources