ruby - Rails + Devise: Undefined Local Variable in UserRegistration#Edit -


update:

i needed put controller code above super, , had access @company instance variable in 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 pass through company id of logged-in user can view users same company.

this page accessed via button edit account page (automatically generated view devise). page in below error:

enter image description here

note: see error button 'manage users' page. if comment out line , @ logs, can see fine:

enter image description here

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 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 }) |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 company controller , views can reach edit_company_users_path ok, , company controller code shown below if that's helpful:

company controller:

class companycontroller < applicationcontroller        def index     @companies = company.all     respond_to |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 ?", @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 |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 note when <%= @company %> in view, no name of company appears (though id 2, , name 'ryan drake'), , displays ok in view. when <%= @company.id %>, throws "undefined method 'id' nil:class".

any guidance accessing company id edit account devise view in easiest way possible, can pass through parameter , hit method superb.

in code show, there end , if, think closing "each" loop before line of error, "company" variable not found.

edit:

i see 2 potential problems:

  1. it @company in edit_company_users_path(@company). if doesn't work, show same error? please show that.

  2. i think might messing devise routes in alias "edit_company_users_path", don't know how organizing resources. if nesting companies under users, default route edit_company_users_path(@company, current_user). run rake routes if routing error.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -