0

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

<a href="/users/sign_in">SIGN IN</a>

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> 
<a href="/users/sign_in">SIGN IN</a>

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'
Community
  • 1
  • 1
pgarber
  • 7
  • 2

1 Answers1

0

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 %>
Benjamin Manns
  • 9,028
  • 4
  • 37
  • 48
  • Thanks. Yes, using the contacts_new_path helper fixed the problem. Any ideas on why my old { controller: ..., action: ...} format causes problems? Also, thanks for the advice on the routes format, and how it affects my path helper – pgarber Oct 28 '15 at 21:42
  • I'm not sure. Using `{ controller: ..., action: ... }` invokes [url_for](http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for). Perhaps that plus some namespacing logic is affecting the namespace that's used for your URL. Unfortunately, I'm not familiar enough to speak definitively. – Benjamin Manns Oct 28 '15 at 21:46