【问题标题】:undefined method `company' for nil:NilClassnil:NilClass 的未定义方法“公司”
【发布时间】:2018-12-29 07:31:50
【问题描述】:

我在主页上不断收到错误提示

'nil:NilClass'的未定义方法`company'

为下面显示的以下代码的第一行。我不太确定如何解决这个问题。我为companiescustomers 有两个控制器,因为我正在创建一个双面市场。

当用户单击顶部的徽标而不是被重定向到欢迎页面时,正在呈现登录页面,因为主页是 root_path,但我正在使用 before_action 的设计进行身份验证。

<% if((current_user.company) || (current_user.customer)) %>
   <%= render 'pages/welcome' %>
<% else %>

<% if current_user.is_company %>
   <%= render 'companies/form', company: @company%>
<% else %>

这里是 home.html.erb 文件,其代码是 root_path

<% if((current_user.company) || (current_user.customer)) %>
    <%= render 'pages/welcome' %>
<% else %>

    <% if current_user.is_company %>
        <%= render 'companies/form', company: @company%>
    <% else %>
        <%= render 'customers/form', customer: @customer%>
    <% end %>

<% end %>

这是我的companiesController

class CompaniesController < ApplicationController
    before_action :set_company, only: [:show, :edit, :update, :destroy]

    def index
        @companies = Company.all
    end

    # GET /companies/1
    # GET /companies/1.json
    def show
       @company = Company.find(params[:company_id])
    end

    # GET /companies/new
    def new
       @company = Company.new
    end

    # GET /companies/1/edit
    def edit
    end

    # POST /companies
    # POST /companies.json
    def create
    @company = Company.new(company_params)

      respond_to do |format|

        if @company.save
            format.html { redirect_to @company, notice: 'Company was successfully created.' }
            format.json { render :show, status: :created, location: @company }
        else
            format.html { render :new }
            format.json { render json: @company.errors, status: :unprocessable_entity }
        end
      end
    end

    # PATCH/PUT /companies/1
    # PATCH/PUT /companies/1.json
    def update
      respond_to do |format|
       if @company.update(company_params)
          format.html { redirect_to @company, notice: 'Company was successfully updated.' }
          format.json { render :show, status: :ok, location: @company }
       else
          format.html { render :edit }
          format.json { render json: @company.errors, status: :unprocessable_entity }
       end
     end
   end

   # DELETE /companies/1
   # DELETE /companies/1.json
   def destroy
      @company.destroy
      respond_to do |format|
         format.html { redirect_to companies_url, notice: 'Company was successfully destroyed.' }
         format.json { head :no_content }
      end
   end

   private
      # Use callbacks to share common setup or constraints between actions.
      def set_company
          @company = Company.find(params[:id])
      end

      # Never trust parameters from the scary internet, only allow the 
      white list through.
         def company_params
            params.require(:company).permit(:username, :phone, :website, :street, :city, :state, :country, :user_id)
         end
     end

【问题讨论】:

  • 它只是说current_user为nil,你需要确保用户已登录才能使用current_user.company
  • 哦,是的,这很有效。你知道我是否正确呈现欢迎页面吗?因为在用户输入他们的详细信息后,它仍然被定向到表单页面而不是欢迎页面。
  • 你不会在视图中进行这些类型的验证,只需在控制器上进行那些类型的事情,当你收到表单的参数时,如果一切正常,重定向到主页或再次渲染有错误时的表格
  • 为了克服这个问题你需要检查current_user&amp;.company&amp;在找到调用者对象nil后不会调用所以如果你的current_user.user是nil那么公司方法将不会调用

标签: javascript html css ruby-on-rails ruby


【解决方案1】:

它只是说current_usernil,你需要确保用户登录才能使用current_user.company

【讨论】:

    【解决方案2】:

    日志向您详细说明了公司为何返回零。该公司需要其中有一个对象才能正确加载页面。但事实并非如此,因为没有用户登录,因为公司没有要返回的对象。

    返回 nil,因为没有用户登录。

    可以先检查是否有用户登录,然后渲染 在此之前的东西以防止错误页面

    在此处渲染

    或者只是登录以确保这不会返回零。但第一个解决方案是最理想的。

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多