Rails + Devise: Undefined Local variable in UserRegistration # Edit
UPDATE:
I needed to put my controller code ABOVE super, for example, and then I had access to the @company instance variable in the view:
User Registration Controller:
class UserRegistrationController < Devise::RegistrationsController
before_action :configure_permitted_parameters
skip_before_filter :load_customer_access, only: [:new, :create]
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
end
def edit
@customer = current_user.customer
@company = Company.find_by_domain(User.get_domain(@customer.email))
super
end
end
I want to pass the company id of the current user so that I can view all users from the same company.
This page will be accessed via a button on the Edit Account page (auto-generated view using Develop). This is the page where I am getting the following error:
Note. I only see this error using the User Management button. If I comment out this line and look at my logs, I can see it perfectly:
User model:
...
def self.get_domain(email_address)
email_address.gsub(/.+@([^.]+.+)/, '\1')
end
def confirm!
current_customer = Customer.where(email: self.email).first
super
end
def ensure_has_customer
customer = self.customer
company = Company.find_by_domain(User.get_domain(self.email))
end
...
User Registration Controller:
class UserRegistrationController < Devise::RegistrationsController
before_action :configure_permitted_parameters
skip_before_filter :load_customer_access, only: [:new, :create]
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
end
def edit
super
@customer = current_user.customer
@company = Company.find_by_domain(User.get_domain(@customer.email))
puts "HAPPY CUSTOMER"
puts @customer.email
puts "HAPPY COMPANY"
puts @company.id // See above screenshot for logs
end
end
Change user route:
match "company/:company_id/edit_users" => 'company#edit_users', via: [:get], :as => :edit_company_users
_edit_account.html.erb:
<%= form_for(current_user, :as => :user, :url => registration_path(:user), :html => { :method => :put }) do |f| %>
<div>
<h1 class="login-header">My Account</h1>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.text_field :first_name, :class => 'form-control custom-form-control', :placeholder => 'First name' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.text_field :last_name, :class => 'form-control custom-form-control', :placeholder => 'Last name' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.email_field :email, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'Email' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :password, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'New password' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :password_confirmation, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'New password confirmation' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :current_password, :class => 'form-control custom-form-control', :placeholder => 'Current password' %>
</div>
</div>
</div>
<br>
<div class="form-actions">
<button type="submit" class="btn-account-primary">Update Account</button>
</div>
<% end %>
<% if @customerAccess.admin_role %>
<hr>
<%= link_to 'Manage Users', edit_company_users_path(@company), :class => 'btn btn-sm btn-default' %><br>
<% end %>
The code for my controller and views can reach edit_company_users_path
ok, and the company controller code is shown below if useful:
Company controller:
class CompanyController < ApplicationController
def index
@companies = Company.all
respond_to do |format|
format.html
format.json { render json: @companies }
end
end
def edit_users
@company = Company.find(params[:company_id])
@company_name = @company.name
@domain = @company.domain
@find_domain = "%" + @domain + "%"
@users = User.find(:all, :conditions => ["email LIKE ?", @find_domain])
end
end
View company index
<div class="col-md-12">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="page-header">
<h1>Manage Companies</h1>
</div>
<table class = "table table-striped table-bordered">
<tr>
<th>Company Name</th>
<th>Domain</th>
<th>Action</th>
</tr>
<% @companies.each do |company| %>
<tr>
<td><%= company.name %></td>
<td><%= company.domain %></td>
<td>
<%= link_to 'Manage Company Access', edit_company_path(company), :class => 'btn btn-sm btn-default' %>
<%= link_to 'Manage Applications', edit_company_applications_path(company), :class => 'btn btn-sm btn-default' %>
<%= link_to 'Manage Users', edit_company_users_path(company), :class => 'btn btn-sm btn-default' %><br>
</td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
It is interesting to note that when I do <%= @company %>
in the view, the Company name does not appear (although the ID is 2 and the name is "Ryan Drake") and is displayed in view mode. When I do <%= @company.id %>
, it throws out "undefined method" id "for nil: class".
Any guide to accessing the Company ID from the Edit Account view in the easiest way, so I can pass that through a parameter and hit the method would be excellent.
source to share
In the code you are showing there is an end and a if I think you are probably closing "every" loop before the error line, so the variable "company" was not found.
EDIT:
I see two potential problems:
-
It's definitely
@company
inedit_company_users_path(@company)
. If it doesn't work, does it show the same error? Please show it. -
I think you can tinker with development routes in the alias "edit_company_users_path", but I don't know how you organize your resources. If you are hosting companies under users, the default route for this would be something like edit_company_users_path (@company, current_user). Run
rake routes
if you get a routing error.
source to share