【问题标题】:SyntaxError in FoodTablesController#indexFoodTablesController 中的语法错误#index
【发布时间】:2015-08-04 10:04:51
【问题描述】:

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

:/Users/RubymineProjects/FoodDBScript/app/controllers/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

【问题讨论】:

  • 请格式化您的代码sn-p。
  • 你有什么问题?
  • 我尝试复制代码并运行它。没有语法错误。要么指定问题,要么可以关闭

标签: ruby-on-rails ruby


【解决方案1】:

您在提供的 sn-p 中没有语法错误。

但鉴于您的错误信息:您不应该使用:Edible_part_%,这不是符号的正确名称。

请改用:edible_part_percentage

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2015-06-19
    相关资源
    最近更新 更多