强参数抛出“未知密钥”错误

时间:2013-09-18 16:25:13

标签: ruby-on-rails ruby-on-rails-4 strong-parameters

我试图让这个模型使用强参数,但我似乎无法弄清楚我做错了什么!?

ERROR:

Unknown key: name

控制器:

class PracticesController < ApplicationController

    def practice_params
        if params[:action] == 'create'
            params.require(:practice).permit( :name, :billing_address, :physical_address, :phone_number, :fax_number, :emergency_contact, :emergency_phone, :email_addres, :active, :ABN, :time_zone)
        elsif params[:action] == 'update'
            params.require(:practice).permit( :name, :billing_address, :physical_address, :phone_number, :fax_number, :emergency_contact, :emergency_phone, :email_addres, :active, :ABN, :time_zone)
        end
    end

    # GET /practices
  # GET /practices.json
  def index
    @practices = Practice.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @practices }
    end
  end

  # GET /practices/1
  # GET /practices/1.json
  def show
    @practice = Practice.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @practice }
    end
  end

  # GET /practices/new
  # GET /practices/new.json
  def new
    @practice = Practice.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @practice }
    end
  end

  # GET /practices/1/edit
  def edit
    @practice = Practice.find(params[:id])
  end

  # POST /practices
  # POST /practices.json
  def create
    @practice = Practice.new(practice_params)

    respond_to do |format|
      if @practice.save
        format.html { redirect_to @practice, notice: 'Practice was successfully created.' }
        format.json { render json: @practice, status: :created, location: @practice }
      else
        format.html { render action: "new" }
        format.json { render json: @practice.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /practices/1
  # PUT /practices/1.json
  def update
    @practice = Practice.find(practice_params)

    respond_to do |format|
      if @practice.update_attributes(practice_params)
        format.html { redirect_to @practice, notice: 'Practice was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @practice.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /practices/1
  # DELETE /practices/1.json
  def destroy
    @practice = Practice.find(params[:id])
    @practice.destroy

    respond_to do |format|
      format.html { redirect_to practices_url }
      format.json { head :ok }
    end
  end
end

型号:

class Practice < ActiveRecord::Base
  #has_many :providers, :patients, :employees, :operatories, :clinics, :patients, 
  has_many :imagecategories
    validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)

    def initial_setup
        Operatory.create(:name => "Default Operatory", :active => true, :practice => self)

        ####### Create Practice Basic Accounts:
        Revenue.create(:name => "General Revenue", :practice => self)

    end

end

架构(相关部分):

  create_table "practices", force: true do |t|
    t.string   "name"
    t.text     "billing_address"
    t.text     "physical_address"
    t.string   "phone_number"
    t.string   "fax_number"
    t.string   "emergency_contact"
    t.string   "emergency_phone"
    t.string   "email_address"
    t.boolean  "active"
    t.string   "ABN"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "time_zone"
  end

任何帮助解决这个问题都会非常感激,也许我很累,只是错过了一些明显的东西。但我从其他正常工作的控制器复制了大部分代码,并且我已经做了所有必要的调整。任何帮助将不胜感激

PS它不仅限于:name参数,当我删除它时,它只是开始抱怨:billing_address

1 个答案:

答案 0 :(得分:1)

在更新操作中,更改

@practice = Practice.find(practice_params)

@practice = Practice.find(params[:id])