2016-09-13 4 views
0

は私が@Subdomainというクラスを持っている:ここではレール - 条件ビューの場合 - nilのための未定義のメソッド `ID」: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はokです存在する場合、すべてが正常に動作しますが、それが存在しない場合、私はこのエラーメッセージが表示されます。

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

これを修正するにはどうすればよいですか?おかげ

+0

'NoMethodErrorインデックスindex':

は私がもう一つは、そのようなあなたのビューとなり、ループ内で私のif...else文を包むのですか?これはどのような見方ですか?インデックスには '@subdomains'(複数形)しかありません。あなたはすべてのサブドメインをループしようとしていますか? – SteveTurczyn

+0

私はlayouts/partials/_topnavbar.html.erbにあります。例えば、他のアドレスに行った場合:NoMethodError in Devise :: Sessions#new –

+0

モデルとスキーマを表示できますか? –

答えて

3

は、迅速かつ汚いソリューションはちなみに#try(ただしお勧めしません)

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

を使用することです、あなたの代わりに、コードを読みやすくするために#blank?を否定の#present?を使用することができます。

ただし、私が正しく仮定して@subdomainがActiveRecordモデルである場合、IDが存在するかどうかを確認する必要はありません。 @subdomainが見つからない場合、それはとにかくnilなります

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

- その後、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?に否定する。

ビューの最初の行を変更します。すなわち、そのあなたのindexアクションに気を取られているので、間違いなくあなたのサブドメインのテーブルの上にすべての属性を返します

<% if @subdomains %>

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

、、。サブドメイン#で

<% @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 %> 
関連する問題