我有一個名爲類@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">
  <%= @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">
  <%= @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]?) %>
我能做些什麼來解決這個問題?感謝
'在子域#NoMethodError index'指數?這是什麼意見?索引只有'@ subdomains'(複數)。你是否想要遍歷所有的子域? – SteveTurczyn
我在佈局/ partials/_topnavbar.html.erb,例如,如果我去其他地址,我得到:NoMethodError in Devise :: Sessions#new –
你能展示你的模型和模式嗎? –