Reputation: 369
I needed to put my controller code ABOVE super, like so 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 through the Company ID of the currently logged-in user so I can then view all the users from the same company.
This page will be accessed via a button from the Edit Account page (automatically generated view with Devise). This is the page in which I get the below error:
Note: I only see this error with the button to 'Manage Users' page. If I comment out that line and look at my logs, I can see this fine:
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
Edit 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 Company controller and views can reach the edit_company_users_path
ok, and the Company controller code is shown below if that's helpful:
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
Company Index View:
<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>
What's interesting to note is that when I do <%= @company %>
in the view, no name of the Company appears (though the ID is 2, and name is 'Ryan Drake'), and displays ok in the view. When I do <%= @company.id %>
, it throws "undefined method 'id' for nil:class".
Any guidance about accessing the company ID from the Edit Account Devise view in the easiest way possible, so I can pass that through as a parameter and hit the method would be superb.
Upvotes: 3
Views: 117
Reputation: 1021
In the code you show, there is an end and a if, I think you are probably closing the "each" loop before the line of the error, so the "company" variable is not found.
EDIT:
I see two potential problems:
It is definitely @company
in edit_company_users_path(@company)
. If
it doesn't work, does it show the same error? Please show that.
I think you might be messing with devise routes in the alias
"edit_company_users_path", but I don't know how are you organizing
your resources. If you are nesting companies under users, the
default route for that would be something like
edit_company_users_path(@company, current_user). Run rake routes
if
you get a routing error.
Upvotes: 1