尝试构建一条路线,可以显示各种类型糖果的不同信息页面。
路线识别网址路径,但希望它只显示有效的糖果类型,例如kit_kat,gummy_bear,twizzler指定的任何其他类型的糖果应生成404状态代码
生成一个脚手架,允许任何人添加糖果类型,但当我尝试传递有效的糖果类型(kit_kat等)时,我收到错误
Rails 4.2 CandiesController中的NameError #create 未定义的局部变量或方法`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_controller.rb
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
答案 0 :(得分:2)
有几件事,
首先,params没有:title
,:title
位于params[:candy][:title]
,或者您只使用candy_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
不存在,空字符串或错误类型