Arnas Klasauskas
Arnas Klasauskas

Reputation: 19

Rails: devise_invitable not accepting variables

I'm currently trying to show all users that were invited by the current user, but this doesn't work since @users is always nil.

I'm trying this on http://localhost:3000/inivtation/new

My InvitationsController

...
    def new
     super
     @users = User.all # This also doesn't work
    end
...

My invitations/new.html.erb

<%= @users.each do |user| %>
 <%= image_tag avatar_url(user) %>
 <%= user.fullname %>
<% end %>

The error:

ActionView::Template::Error (undefined method `each' for nil:NilClass):

Upvotes: 0

Views: 65

Answers (1)

codingnightmare
codingnightmare

Reputation: 56

There are lots of way to fixing your issue. Please look at my views and choose any of the appropriate one that you feel good.

Method. 1 ( Simplest )

Please just use this code and it will work for you simply :)

<%= User.all.each do |user| %>
 <%= image_tag avatar_url(user) %>
 <%= user.fullname %>
<% end %>

Method. 2 ( Ok but not best )

Your InvitationsController

class InvitationsController < Devise::InvitationsController
 def new   
   @users = User.all
   super
 end
end

Your invitations/new.html.erb

<%= @users.each do |user| %>
 <%= image_tag avatar_url(user) %>
 <%= user.fullname %>
<% end %>

Method. 3 ( Best )

Your InvitationsController

class InvitationsController < Devise::InvitationsController
  def new
    self.resource = resource_class.new
    @users = User.all
    render :new
  end
end

Your invitations/new.html.erb

<%= @users.each do |user| %>
 <%= image_tag avatar_url(user) %>
 <%= user.fullname %>
<% end %>

Overriding Devise Routes for Method. 2 and Method. 3

Please make sure about overriding the default invitations controller in your routes :)

# config/routes.rb
devise_for :users, controllers: {
  invitations: "invitations"
}

Upvotes: 2

Related Questions