Ramy
Ramy

Reputation: 21261

Where should I set the current_user so I don't get a missing key error?

My question assumes a specific answer but it may not but accurate.

I've created a route:

get 'users/:id/groups/' => 'users#groups', as: "my_groups"

but when I go to access this path, conditionally, in a view, i get an exception:

ActionController::UrlGenerationError at /
No route matches {:controller=>"users", :action=>"groups"} missing required keys: [:id]

Here is the partial with the offending line:

<% if user_signed_in? %>
  <li id="groups-menu" class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
       Groups <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
    #offending line:
        <li><%= link_to 'My Groups', my_groups_path %></li>
        <li><%= link_to 'Create a Group', new_group_path  %></li>
    </ul>
  </li>
  <li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
  <li><%= link_to 'Login', new_user_session_path %></li>
<% end %>
<% if user_signed_in? %>
  <li><%= link_to 'Edit account', edit_user_registration_path %></li>
<% end %>
<% if user_signed_in? %>
  <% if current_user.has_role? :admin %>
    <li><%= link_to 'Admin', users_path %></li>
  <% end %>
<% end %>

it seems that there is no current_user set, perhaps?

It's unclear how this could break if the user_signed_in? method is working.

Upvotes: 0

Views: 64

Answers (1)

Anurag Abbott
Anurag Abbott

Reputation: 364

You need to pass in the :id parameter to the url helper function

<li><%= link_to 'My Groups', my_groups_path(current_user) %></li>

Upvotes: 2

Related Questions