gpt4 book ai didi

ruby-on-rails - Rails + 设计 : Undefined Local Variable in UserRegistration#Edit

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:08 24 4
gpt4 key购买 nike

更新:

我需要将我的 Controller 代码放在 ABOVE super 中,就像这样,然后我可以访问 View 中的 @company 实例变量:

用户注册 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

我想传递当前登录用户的公司 ID,这样我就可以查看同一公司的所有用户。

此页面将通过“编辑帐户”页面(使用 Devise 自动生成的 View )中的按钮访问。这是我收到以下错误的页面:

enter image description here

注意:我只在“管理用户”页面的按钮上看到此错误。如果我注释掉该行并查看我的日志,我可以看到这一点:

enter image description here

用户模型:

   ...
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
...

用户注册 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

编辑用户路线:

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 %>

我的公司 Controller 和 View 的代码可以到达 edit_company_users_path好的,如果有帮助,公司 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 :

<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>

有趣的是,当我执行 <%= @company %> 时在 View 中,没有出现公司名称(尽管 ID 为 2,名称为“Ryan Drake”),并且在 View 中显示正常。当我做 <%= @company.id %> ,它抛出“nil:class 的未定义方法‘id’”。

任何关于以最简单的方式从“编辑帐户设计” View 访问公司 ID 的指导,以便我可以将其作为参数传递并点击该方法将是极好的。

最佳答案

在您显示的代码中,有一个结束和一个 if,我认为您可能在错误行之前关闭了“each”循环,因此未找到“company”变量。

编辑:

我看到两个潜在的问题:

  1. 肯定是edit_company_users_path(@company)中的@company。如果它不起作用,是否显示相同的错误?请证明这一点。

  2. 我认为你可能在别名中搞乱了设计路线“edit_company_users_path”,但我不知道你是如何组织的你的资源。如果您在用户下嵌套公司,则默认路线是这样的编辑公司用户路径(@公司,当前用户)。运行 rake routes 如果你收到路由错误。

关于ruby-on-rails - Rails + 设计 : Undefined Local Variable in UserRegistration#Edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30566314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com