FoodTablesController#index中的SyntaxError

时间:2015-08-04 10:04:52

标签: ruby-on-rails ruby

我正在使用RoR在食物成分表上工作。当我尝试在localhost上查看表时,我收到以下错误

  

:/用户/ RubymineProjects / FoodDBScript /应用/控制器/ food_tables_controller.rb:72:   语法错误,意外','... ID,:Food_Item,:Edible_part_%,   :Water_g,:Kilojoules_kj,:K ...... ...... ^

这是我的food_tables_controller.rb

的一部分
       class FoodTablesController < ApplicationController
   before_action :set_food_table, only: [:show, :edit, :update,     :destroy]

     # GET /food_tables
   # GET /food_tables.json
   def index
    if params[:food_item].nil?
     @food_tables = FoodTable.all
    else
     @food_tables = FoodTable.where("Food Item LIKE ?", '%' +    params[:food_item] + '%')
    end
    end

   # GET /food_tables/1
   # GET /food_tables/1.json
   def show
   end

   # GET /food_tables/new
   def new
    @food_table = FoodTable.new
  end

  # GET /food_tables/1/edit
 def edit
 end

  # POST /food_tables
 # POST /food_tables.json
 def create
   @food_table = FoodTable.new(food_table_params)

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

 # PATCH/PUT /food_tables/1
 # PATCH/PUT /food_tables/1.json
 def update
   respond_to do |format|
  if @food_table.update(food_table_params)
    format.html { redirect_to @food_table, notice: 'Food table was successfully updated.' }
    format.json { render :show, status: :ok, location: @food_table }
  else
    format.html { render :edit }
    format.json { render json: @food_table.errors, status: :unprocessable_entity }
  end
  end
 end

 # DELETE /food_tables/1
# DELETE /food_tables/1.json
 def destroy
   @food_table.destroy
   respond_to do |format|
  format.html { redirect_to food_tables_url, notice: 'Food table was  successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
 # Use callbacks to share common setup or constraints between actions.
def set_food_table
  @food_table = FoodTable.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def food_table_params
  params[:food_table]
     end
  end

1 个答案:

答案 0 :(得分:0)

您在提供的代码段中没有语法错误。

但是给出了您的错误消息:您不应该使用:Edible_part_%,这不是符号的正确名称。

改为使用:edible_part_percentage

虽然您可以使用:Edible_part_percentage第一个带有大写字母,甚至:'Edible_part_%'带引号作为符号名称,但您会发现遵循Rails命名约定更有用。