Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails CandiesController#create中的Rails 4.2名称错误_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails CandiesController#create中的Rails 4.2名称错误

Ruby on rails CandiesController#create中的Rails 4.2名称错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,尝试构建一个路由,该路由可以显示包含各种类型糖果的各种信息的页面 路由识别URL路径,但希望它仅显示有效的糖果类型,例如kit_kat、gummy_bear、twizzler。指定的任何其他类型的糖果应生成404状态代码 生成了一个脚手架,允许任何人添加糖果类型,但当我尝试传递有效的糖果类型(kit_kat等)时,我得到了错误 CandiesController#create中的Rails 4.2名称错误 未定义的局部变量或方法“params”# 有几件事 首先,params没有:title,:

尝试构建一个路由,该路由可以显示包含各种类型糖果的各种信息的页面

路由识别URL路径,但希望它仅显示有效的糖果类型,例如kit_kat、gummy_bear、twizzler。指定的任何其他类型的糖果应生成404状态代码

生成了一个脚手架,允许任何人添加糖果类型,但当我尝试传递有效的糖果类型(kit_kat等)时,我得到了错误

CandiesController#create中的Rails 4.2名称错误 未定义的局部变量或方法“params”#

有几件事

首先,params没有
:title
:title
params[:candy][:title]
中,或者您只需使用
candy\u params[:title]

其次,if语句可以更短

if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
      :twizzler].include?(candy_params[:title].to_sym)
  (Go on and create the candy)
else
  (Redirect with error messages | Wrong Candy Type)
end
最好先检查参数是否存在并确保它不是空的,然后检查它是否包含在可接受列表中。注意,您的原始代码是比较符号和字符串,所以将它们转换为相同的类型并进行检查

更新


添加了当
:title
不存在、空字符串或错误类型时重定向的else语句

谢谢,对:title感到困惑,但现在得到未定义的局部变量或方法“candy”,用于#在此行->if candy[:title]&&!糖果[:标题]。空?&&[:kit_kat,:skittles,:m_and_ms,:herseys_kiss,:butterfinger,:gummy_bear,my bad,typo,这是
candy_params
没有问题,更新了代码,最后一件事,它允许我添加有效的糖果类型,例如kit_kat,但也可以添加无效的糖果类型,例如snickers?我需要用错误消息重定向吗?更新了,添加了else语句无法放置带有错误消息的重定向
class Candy < ActiveRecord::Base

  extend FriendlyId
  friendly_id :title, use: :slugged 

end
    def create
        if candy[:title] && !candy[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
          :twizzler].include?(candy[:title].to_sym)

        @candy = Candy.new(candy_params)

        respond_to do |format|
          if @candy.save
            format.html { redirect_to @candy, notice: 'Candy was successfully created.' }
            format.json { render :show, status: :created, location: @candy }
          else
            format.html { render :new }
            format.json { render json: @candy.errors, status: :unprocessable_entity }
          end
       end
      end
    end

updated code

def create
  if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
      :twizzler].include?(candy_params[:title].to_sym)


    @candy = Candy.new(candy_params)

    respond_to do |format|
      if @candy.save
        format.html { redirect_to @candy, notice: 'Candy was successfully created.' }
        format.json { render :show, status: :created, location: @candy }
      else
        format.html { render :new }
        format.json { render json: @candy.errors, status: :unprocessable_entity }
      end
   end
 end
end
if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
      :twizzler].include?(candy_params[:title].to_sym)
  (Go on and create the candy)
else
  (Redirect with error messages | Wrong Candy Type)
end