簡體   English   中英

未定義的方法“公司”為 nil:NilClass

[英]undefined method `company' for nil:NilClass

我一直在我的主頁上收到一個錯誤說

'未定義的方法'公司'為 nil:NilClass'

對於下面顯示的以下代碼的第一行。 我不太確定如何解決這個問題。 我有兩個負責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 %>

這是我的公司控制器

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_usernil ,您需要確保用戶已登錄才能使用current_user.company

該日志為您提供了有關公司返回零的原因的詳細信息。 該公司需要其中的一個對象才能正確加載頁面。 但事實並非如此,因為沒有用戶登錄公司有返回對象。

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

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

返回 nil 因為沒有用戶登錄。

您可以先檢查是否有用戶登錄,然后在此之前渲染一些內容以防止出現錯誤頁面

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

<% if current_user.is_company %> <%= render 'companys/form', company: @company%> <% end %> <% else %> 在這里渲染一些東西 <% end %>

或者簡單地登錄以確保這不會返回零。 但第一個修復是最理想的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM