我正在制作一个简单的Rails应用程序来代表办公室里的人 - 每个人都有姓名,电子邮件,桌面号码等。
我的问题绝对是我忽略的简单问题 - 当我创建一个新人并给他们一个桌面编号,或者更新现有人员以使用浏览器中的应用程序更改桌面编号时,不会进行更改考虑到 - 我只能在控制台中更新桌面号码。
我将“桌面”属性单独添加为事后补充,而不是在创建“人物”时,所以我认为那里有一些我错过的东西。
show.html.haml:
%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
除了'桌面'之外,这里的所有元素都更新。
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
示例场景1:
示例方案2:
在rails控制台中,我这样做:
p = Person.find_by(id:2)
p.update_attribute:desk,10
我是否在那里包含了所有必要的信息?感谢任何可以提供帮助的人!
编辑 - controller.rb:
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
form.html.haml
= simple_form_for(@person, :html => {:multipart => true, class: 'form-horizontal' }) do |f|
- if @person.errors.any?
#error_explanation
%h2
= pluralize(@person.errors.count, "error")
prohibited this person from being saved:
%ul
- @person.errors.full_messages.each do |msg|
%li= msg
.box
.form-inputs
= f.input :name
= f.input :photo
= f.input :position
= f.input :email
= f.input :irc
= f.input :desk
.form-actions.well
= f.button :submit, class: 'btn btn-success'
= link_to 'Back', people_path, class: 'btn'
答案 0 :(得分:2)
您未将desk
放入允许的参数中:
def person_params
params.require(:person).permit(:name, :photo, :position, :email, :irc, :desk)
end