は私が@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">
  <%= @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はokです存在する場合、すべてが正常に動作しますが、それが存在しない場合、私はこのエラーメッセージが表示されます。
NoMethodError in Subdomains#index
undefined method `id' for nil:NilClass
<% if ([email protected]?) %>
これを修正するにはどうすればよいですか?おかげ
'NoMethodErrorインデックスindex':
は私がもう一つは、そのようなあなたのビューとなり、ループ内で私の
if...else
文を包むのですか?これはどのような見方ですか?インデックスには '@subdomains'(複数形)しかありません。あなたはすべてのサブドメインをループしようとしていますか? – SteveTurczyn私はlayouts/partials/_topnavbar.html.erbにあります。例えば、他のアドレスに行った場合:NoMethodError in Devise :: Sessions#new –
モデルとスキーマを表示できますか? –