2016-09-13 41 views
0

我有一個名爲類@subdomain:軌 - 如果條件對視圖 - 未定義的方法'身份證」的零:NilClass

這裏是我的控制器:

class SubdomainsController < ApplicationController 
 
    before_action :authenticate_user!, only: [:edit, :update] 
 
    before_action :set_subdomain, only: [:show, :edit, :update, :destroy] 
 
    before_action :redirect_to_subdomain, only: :show 
 
    before_action :redirect_to_subdomain_show, only: :root, unless: '@subdomain.nil?' 
 

 
    def root 
 
    render nothing: true 
 
    end 
 

 
    def index 
 
    @subdomains = Subdomain.all 
 
    end 
 

 
    def show 
 
    end 
 

 
    def new 
 
    @subdomain = Subdomain.new 
 
    end 
 

 
    def edit 
 
    end 
 

 
    def create 
 
    @subdomain = Subdomain.new(subdomain_params) 
 

 
    respond_to do |format| 
 
     if @subdomain.save 
 
     format.html { redirect_to @subdomain, notice: 'Subdomain was successfully created.' } 
 
     format.json { render :show, status: :created, location: @subdomain } 
 
     else 
 
     format.html { render :new } 
 
     format.json { render json: @subdomain.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 

 
    def update 
 
    respond_to do |format| 
 
     if @subdomain.update(subdomain_params) 
 
     format.html { redirect_to @subdomain, notice: 'Subdomain was successfully updated.' } 
 
     format.json { render :show, status: :ok, location: @subdomain } 
 
     else 
 
     format.html { render :edit } 
 
     format.json { render json: @subdomain.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 

 
    def destroy 
 
    @subdomain.destroy 
 
    respond_to do |format| 
 
     format.html { redirect_to subdomains_url, notice: 'Subdomain was successfully destroyed.' } 
 
     format.json { head :no_content } 
 
    end 
 
    end 
 

 
    private 
 

 
    def redirect_to_subdomain_show 
 
    redirect_to subdomain_url(@subdomain) if @request_subdomain.eql? @subdomain.address 
 
    end 
 

 
    # Redirect to subdomain... if the current one isn't equal to @subdomain.address column 
 
    def redirect_to_subdomain 
 
    redirect_to subdomain_url(@subdomain, subdomain: @subdomain.address) unless @request_subdomain.eql? @subdomain.address 
 
    end 
 

 
    def set_subdomain 
 
    @subdomain = Subdomain.find(params[:id]) 
 
    end 
 

 
    def subdomain_params 
 
    @subdomain.assign_attributes(tag_speciality: params[:subdomain][:tag_speciality].split(',')) 
 
    params.require(:subdomain).permit(
 
     :domain_id, 
 
     :address, 
 
     :title, 
 
     :description, 
 
     :is_published, 
 
     :button_text_client, 
 
     :button_text_service, 
 
     :cover, 
 
     :primary_colour, 
 
     :secundary_colour, 
 
     :contrast_colour, 
 
     :logo, 
 
     :find_clients, 
 
     :find_professional, 
 
     :start_relation, 
 
     :publications_wall, 
 
     :tag_speciality, 
 
     :cards_wall 
 
    ) 
 
    end 
 
end

在視圖我創造了一個條件:

 <% if ([email protected]?) %> 
 
     <li> 
 
      <%= link_to cards_from_subdomain_path(subdomain_id: @subdomain.id) do %> 
 
       <i class="fa fa-search fa-lg"></i> 
 
       <span class="hidden-medium"> 
 
       &nbsp <%= @subdomain.cards_wall %> 
 
       </span> 
 
      <% end %> 
 
     </li> 
 
     <li> 
 
      <%= link_to publications_from_subdomain_path(subdomain_id: @subdomain.id) do %> 
 
       <i class="icon-grid fa-lg"></i> 
 
       <span class="hidden-medium"> 
 
        &nbsp <%= @subdomain.publications_wall %> 
 
       </span> 
 
      <% end %> 
 
     </li> 
 
     <% else %> 
 
     <li>Some thing</li> 
 
    <% end %>

這裏是模型:

class Subdomain < ActiveRecord::Base 
 
    belongs_to :domain 
 
    has_many :cards 
 

 
    mount_uploader :cover, AssetsUploader 
 
    mount_uploader :logo, AssetsUploader 
 
end

當存在@subdomain是確定的,一切正常,但是當它不存在,我得到這個錯誤信息:

NoMethodError in Subdomains#index 
undefined method `id' for nil:NilClass 
<% if ([email protected]?) %> 

我能做些什麼來解決這個問題?感謝

+0

'在子域#NoMethodError index'指數?這是什麼意見?索引只有'@ subdomains'(複數)。你是否想要遍歷所有的子域? – SteveTurczyn

+0

我在佈局/ partials/_topnavbar.html.erb,例如,如果我去其他地址,我得到:NoMethodError in Devise :: Sessions#new –

+0

你能展示你的模型和模式嗎? –

回答

3

快速和骯髒的解決方案是使用#try(雖然不推薦)

<% if [email protected](:id).try(:blank?) %> 

順便說一句,你可以用#present?,而不是否定一個#blank?使代碼更易讀。

但是,如果我假設正確並且@subdomain是ActiveRecord模型,則不需要檢查它是否存在id。一個下面的代碼應該是在你的情況充分:

<% if @subdomain %> 
    (...) 
<% else %> 
    (...) 
<% end %> 

如果沒有找到@subdomain,這將是一個nil無論如何 - 然後if @subdomain條件將被評估爲假 - 因爲nil是falsy值。

如果你是因爲你碰巧呈現這樣一個集合得到這個錯誤:

= render 'subdomain', collection: @subdomains 

...那麼你可以把一個條件裏面...

<% if @subdomains.present? %> 
    = render 'subdomain', collection: @subdomains 
<% else %> 
    <% # @subdomains are an empty collection %> 
<% end %> 
1

從您的create action,id肯定會在您的表格中創建。我不明白你在努力達到什麼目的,否定#id.blank?

更改視圖的第一行。即

<% if ([email protected]?) %>

到,

<% if @subdomains %>,這一定會回到你的子域表的所有屬性以來一直在你的索引行動被照顧。

我做的另一件事是換我if...else聲明一個循環中,這樣的你的看法變成:

<% @subdomains.each do |subd| %> 
     <% if subd.id.present? %> #you can use another attribute here instead of id. e.g. <% if subd.is_published == true %> since `is_published` is a boolean. 
      <li> 
       (#your codes here referenced with the format called `subd.whatever_attribute`) 
      </li> 
      <li> 
       (#your codes here referenced with the format called `subd.whatever_attribute`) 
      </li> 
     <% else %> 
      <li>Some thing</li> 
     <% end %> 
    <% end %> 
相關問題