在rails

时间:2017-07-06 04:19:13

标签: ruby-on-rails ruby

我刚刚在床模型中将一列'bedNumber'更改为'bed_number',并且我已经改变了控制器和输入字段中的许可,但仍然,我得到错误“ #undefined方法`bedNumber'#你的意思是? bed_number bed_number? bed_number =“。有人可以帮我调试这个吗?索引文件与新列名称一起工作正常,唯一的问题是添加和编辑。

这是我的床控制器:

class BedsController < ApplicationController
  before_action :set_bed, only: [:show, :edit, :update, :destroy]

  # GET /beds
  def index
    @beds = Bed.all
  end

  # GET /beds/1
  def show
  end

  # GET /beds/new
  def new
    @bed = Bed.new
  end

  # GET /beds/1/edit
  def edit
  end

  # POST /beds
  def create
    @bed = Bed.new(bed_params)

    if @bed.save
      redirect_to @bed, notice: 'Bed was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /beds/1
  def update
    if @bed.update(bed_params)
      redirect_to @bed, notice: 'Bed was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /beds/1
  def destroy
    @bed.destroy
    redirect_to beds_url, notice: 'Bed was successfully destroyed.'
  end

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

    # Only allow a trusted parameter "white list" through.
    def bed_params
      params.require(:bed).permit(:bed_number, :description, :room_id)
    end
end

这是我的架构文件:

create_table "beds", force: :cascade do |t|
    t.string   "bed_number"
    t.string   "description"
    t.integer  "room_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.index ["room_id"], name: "index_beds_on_room_id", using: :btree
  end

这是我的输入视图:

= simple_form_for(@bed) do |f|
  = f.error_notification

  .form-inputs
    = f.input :bed_number, placeholder: 'e.g., Bed-10, Bed-12...etc'
    = f.input :description, placeholder: 'e.g., 1m*2m...etc'
    - if Room.any?
      = f.association :room, default: Room.first.name
    - else
      = f.association :room, prompt: 'Please create a room', disabled: true
      = "No room available yet! Please create one by "
      = link_to 'CLICKING HERE', new_room_path

  .form-actions
    = f.button :submit, class: "btn btn-success"

1 个答案:

答案 0 :(得分:5)

哈哈......原来我忘记在模型文件中更改验证中的列名。