2016-06-07 34 views
1

試圖構建一個可顯示各種類型糖果的不同信息的頁面。Rails 4.2 CandiesController中的NameError#create

路線識別URL路徑,但希望它只是出示有效的糖果種類如kit_kat,gummy_bear,twizzler指定應該生成產生支架允許任何人添加糖類型404個狀態碼

任何其他類型的糖果但是當我試圖通過有效的糖果類型(kit_kat等)我得到錯誤

的Rails 4.2 NameError在CandiesController#創建 未定義的局部變量或方法`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 

回答

2

幾件事情,

首先,PARAMS沒有:title:titleparams[:candy][:title],或者你只是使用​​

二,if語句可能b Ë短

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 

它總是好的檢查則params的存在,並確保它不爲空,然後再檢查它是否包含在可以接受的列表。請注意,您的原始代碼是將符號與字符串進行比較,因此將它們轉換爲相同的類型並進行檢查。

UPDATE

增加了對重定向else語句時:title不存在,空字符串,或糊塗了在錯誤類型

+0

感謝:冠軍,但現在越來越未定義的局部變量或方法'糖果'for# on this line-> if candy [:title] &&!candy [:title] .empty? && [:kit_kat,:skittles,:m_and_ms,:herseys_kiss,:butterfinger,:gummy_bear, – Neil

+0

我的不好,錯字,它是'candy_params' – lusketeer

+0

沒有問題,更新的代碼,最後一件事,它允許我添加有效的糖果類型例如kit_kat,但也有無效的糖果類型,例如竊笑?我需要一些重定向與錯誤消息? – Neil

相關問題