尝试构建一个路由,该路由可以显示包含各种类型糖果的各种信息的页面。
路由识别URL路径,但希望它只显示有效的糖果类型,例如kit_kat,gummy_bear,twizzler指定的任何其他类型的糖果应生成404状态代码
生成了一个脚手架,允许任何人添加糖果类型,但当我尝试传递有效的糖果类型(kit_kat等)时,我得到了错误
Rails 4.2 CandiesController中的NameError#为创建未定义的局部变量或方法“params”#
**candy_controller.rb**
class CandiesController < ApplicationController
before_action :set_candy, only: [:show, :edit, :update, :destroy]
# GET /candies
# GET /candies.json
def index
@candies = Candy.all
end
# GET /candies/1
# GET /candies/1.json
def show
end
# GET /candies/new
def new
@candy = Candy.new
end
# GET /candies/1/edit
def edit
end
# POST /candies
# POST /candies.json
def create
if (([:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler]).any? { |word| params[:title].includes?(word) })
@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
# PATCH/PUT /candies/1
# PATCH/PUT /candies/1.json
def update
respond_to do |format|
if @candy.update(candy_params)
format.html { redirect_to @candy, notice: 'Candy was successfully updated.' }
format.json { render :show, status: :ok, location: @candy }
else
format.html { render :edit }
format.json { render json: @candy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /candies/1
# DELETE /candies/1.json
def destroy
@candy.destroy
respond_to do |format|
format.html { redirect_to candies_url, notice: 'Candy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_candy
@candy = Candy.friendly.find(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def candy_params
params.require(:candy).permit(:title, :discription)
end
end
candy.rb
class Candy < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
end
更新了candy/U控制器。铷
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
有几件事,
首先,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语句