我正在制作一个简单的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
[email protected]
除“ 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'
您没有输入desk
允许的参数:
def person_params
params.require(:person).permit(:name, :photo, :position, :email, :irc, :desk)
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句