我在控制台中得到了这些参数:
classpath 'com.android.tools.build:gradle:3.2.1'
当我用以下方法检查数组是否为空时:
category_id" # => ["", "14", "16", "17"]
它返回以下错误:
@category = category_id.any?
我有这样的强参数:
undefined method `any?' for nil:NilClass
关于如何检查此数组是否为空的任何想法?
:category_id => []
控制台结果:
def new
@account = Account.new
@categories.build
end
def create
@account = Account.new(account_params)
respond_to do |format|
if @account.save
format.html { redirect_to accounts_path, notice: 'Account was successfully added.' }
format.json { render :index, status: :created, location: @account }
else
format.html { render :new }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
def account_params
params.require(:account).permit(:name, :street, :street_2, :city, :state, :postal_code, :country, category_id: []
end
答案 0 :(得分:0)
@变量在模型内部不可用。也不是参数。
不尝试这样做是一个好习惯,但是以下是一些方法:
如果要传递的值是数据库字段/列:
# In the controller
account = Account.new(:category => @category)
# In this example, :category is a database field/column of the
# Account model, not sure if that's accurate for you.
否则,您必须像这样使用attr_accessor:
# In the model
class Account < ActiveRecord::Base
attr_accessor :category
#In the controller (this is equivalent to the first example)
account = Account.new
account.category = @category
使用attr_accessor不会将任何内容保存到数据库中,但是控制器不知道区别(这很好)。这使您可以随意移动数据。