Ruby on rails Rails:一个属性只会在控制台中更新,而不会在浏览器中更新
我正在制作一个简单的Rails应用程序来代表办公室里的人——每个人都有名字、电子邮件、办公桌号码等 我的问题绝对是我忽略了的一个简单问题——当我创建一个新的人并给他们一个办公桌号码,或者使用浏览器中的应用程序更新一个现有的人来更改办公桌号码时,这个更改没有被考虑在内——我只能在控制台中更新办公桌号码 我在创建“person”时没有添加“desk”属性,而是在事后单独添加了“desk”属性,因此我认为我遗漏了一些东西 show.html.haml:Ruby on rails Rails:一个属性只会在控制台中更新,而不会在浏览器中更新,ruby-on-rails,Ruby On Rails,我正在制作一个简单的Rails应用程序来代表办公室里的人——每个人都有名字、电子邮件、办公桌号码等 我的问题绝对是我忽略了的一个简单问题——当我创建一个新的人并给他们一个办公桌号码,或者使用浏览器中的应用程序更新一个现有的人来更改办公桌号码时,这个更改没有被考虑在内——我只能在控制台中更新办公桌号码 我在创建“person”时没有添加“desk”属性,而是在事后单独添加了“desk”属性,因此我认为我遗漏了一些东西 show.html.haml: %h1 = @person.name .r
%h1
= @person.name
.row
.span4.avatar-placeholder
%p [photo coming soon...]
.span8
%table{class: 'table table-striped'}
%tbody
%tr
%th Name
%td
= @person.name
%tr
%th Position
%td
= @person.position
%tr
%th Email
%td
= @person.email
%tr
%th Irc
%td
= @person.irc
%tr
%th Desk
%td
=@person.desk
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
# GET /people
# GET /people.json
def index
@people = Person.all
end
# GET /people/1
# GET /people/1.json
def show
end
# GET /people/new
def new
@person = Person.new
end
# GET /people/1/edit
def edit
end
# POST /people
# POST /people.json
def create
@person = Person.new(person_params)
respond_to do |format|
if @person.save
format.html { redirect_to @person, notice: 'Person was successfully created.' }
format.json { render action: 'show', status: :created, location: @person }
else
format.html { render action: 'new' }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /people/1
# PATCH/PUT /people/1.json
def update
respond_to do |format|
if @person.update(person_params)
format.html { redirect_to @person, notice: 'Person was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
# DELETE /people/1
# DELETE /people/1.json
def destroy
@person.destroy
respond_to do |format|
format.html { redirect_to people_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_person
@person = Person.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def person_params
params.require(:person).permit(:name, :photo, :position, :email, :irc)
end
end
除了“桌子”之外,这里的所有元素都会很好地更新
schema.db
ActiveRecord::Schema.define(version: 20131104165816) do
create_table "people", force: true do |t|
t.string "name"
t.string "photo"
t.string "position"
t.string "email"
t.string "irc"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar"
t.integer "desk"
end
end
我在person.rb的模型中还没有任何东西:
class Person < ActiveRecord::Base
end
您没有将
桌面放在允许的参数中:
def person_params
params.require(:person).permit(:name, :photo, :position, :email, :irc, :desk)
end
请添加控制器代码和表单代码。show
操作的代码在这里是完全不相关的。控制器和表单代码添加了hanks,我没有意识到如果我添加了额外的属性,我需要更新我的person\u参数@马雷克·利普卡:好地方!我认为如果您试图创建/更新记录,但没有在接受的参数中指定其中一个属性,Rails就会抛出错误。它只是将其作为警告放在服务器日志中吗?
def person_params
params.require(:person).permit(:name, :photo, :position, :email, :irc, :desk)
end