1

v3.2.1

「カウント」がゼロになり、インデックスがレンダリングされない理由がわかりません。「カウント」は、スコープ検証で一意性を確認するまで、すべてのモデルで問題ないためです。

助言がありますか?


モデル

Class FeatureIcon < ActiveRecord::Base
  belongs_to :user

  validates_presence_of :img_size, :feature_name, :image, :user_id
  validates_uniqueness_of :img_size, :scope => :feature_name

  //paperclip interpolates stuff....
end

コントローラ

before_filter :load_user

def index
  @feature_icons = @user.feature_icons.all
  @feature_icon = @user.feature_icons.new

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @feature_icons }
    format.js
  end
end


def create
  @feature_icon = @user.feature_icons.new(params[:feature_icon])

  respond_to do |format|
    if @feature_icon.save
      format.html { redirect_to user_feature_icons_url, notice: 'successfully created.' }
      format.json { render json: @feature_icon, status: :created, location: @feature_icon }
      format.js
    else
      format.html { render action: "index" }
      format.json { render json: @feature_icon.errors, status: :unprocessable_entity }
    end
  end
end

エラー

NoMethodError in Feature_icons#create

undefined method `count' for nil:NilClass
  Extracted source (around line #7):

  6:       <div class="count">
  7:         <div id="count" class="feed-count"><%= @feature_icons.count %></div>
  8:       </div>
4

1 に答える 1

2

createメソッドでは( @feature_icons's' を使用して) インスタンス化しますが、使用しているビューでは@feature_icon('s' を使用せずに) インスタンス化し@feature_iconsますnil

保存に失敗した場合、行format.html { render action: "index" }はビューをレンダリングしますindex.htm.erbが、コントローラーのメソッドindexは呼び出されません。試してみてください

if @feature_icon.save
  #... nothing to change
else
  format.html do
    @feature_icons = @user.feature_icons.all
    render action: "index"
  end
  format.json { render json: @feature_icon.errors, status: :unprocessable_entity }
end

また

if @feature_icon.save
  #... nothing to change
else
  format.html { redirect_to :index }
  format.json { render json: @feature_icon.errors, status: :unprocessable_entity }
end
于 2013-01-06T10:53:36.450 に答える