Rails:一个属性只会在控制台中更新,而不会在浏览器中更新

蒸汽测试

我正在制作一个简单的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:

  • 我转到/ people / new,填写所有字段,然后提交
  • 我被重定向到/ people / 3。当我在表单中输入姓名,职位,电子邮件和Irc时,都将显示它们,但Desk没有显示任何内容。进一步调查显示服务台设置为“无”。

示例场景2:

  • 在Rails控制台中,我这样做:

    p = Person.find_by(id:2)

    p.update_attribute:desk,10

  • 我转到/ people / 2,并且服务台号成功显示为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'
马里克·利普卡(Marek Lipka)

您没有输入desk允许的参数:

def person_params
  params.require(:person).permit(:name, :photo, :position, :email, :irc, :desk)
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails:系统进程不会在Rails服务器中启动,但会在Rails控制台中启动

来自分类Dev

广播消息发送后不会在浏览器控制台中显示,actioncable 和 redis 开发环境

来自分类Dev

我的Chrome浏览器不会在控制台上显示这些代码

来自分类Dev

我的 s3 存储桶中的图像不会在浏览器中打开,它们只会下载

来自分类Dev

Firefox不会在浏览器中显示日志

来自分类Dev

Firefox不会在浏览器中显示日志

来自分类Dev

离子服务不会在浏览器中呈现

来自分类Dev

在REACT中通过API构建无限加载时,即使我向下滚动很多,页面号也不会在控制台中更新

来自分类Dev

Function.prototype不会在控制台中显示所有内置属性和方法

来自分类Dev

在Rails控制台中写入文件不会在退出时保存

来自分类Dev

UndoManager的canUndo属性不会在SwiftUI中更新

来自分类Dev

为什么表达式不会在角度控制器中动态更新?

来自分类Dev

C#SelectVoice不会在Windows应用程序中更改,但会在控制台中更改

来自分类Dev

一对多字段不会在JPA中更新

来自分类Dev

Tkinter不会在新目录中将映像文件更新为一个

来自分类Dev

Oracle触发器不会在另一个表中插入值

来自分类Dev

Rails全局变量不会在更新时更新

来自分类Dev

单击Thunderbird中的http(s)链接不会在浏览器中打开该链接

来自分类Dev

不会在IE浏览器的集合中删除Backbone js模型

来自分类Dev

CSS Active Class不会在浏览器中得到尊重吗?

来自分类Dev

我的Chromium浏览器对象不会在Winform中显示

来自分类Dev

此代码不会在浏览器中显示tom

来自分类Dev

更改后刷新页面不会在Web浏览器中呈现

来自分类Dev

正确安装 Xampp 后,它不会在 Kali Linux 的浏览器中打开

来自分类Dev

Cookie 不会在所有浏览器中过期/删除

来自分类Dev

如何生成不会在浏览器中触发警告的自签名证书?

来自分类Dev

Wordpress 主题中的 Bootstrap 不会在浏览器中呈现

来自分类Dev

Fullpage.js 不会在任何浏览器(chrome、firefox、edge)中滚动

来自分类Dev

Dart-运行项目不会加载dart文件,也不会在控制台中打印

Related 相关文章

  1. 1

    Rails:系统进程不会在Rails服务器中启动,但会在Rails控制台中启动

  2. 2

    广播消息发送后不会在浏览器控制台中显示,actioncable 和 redis 开发环境

  3. 3

    我的Chrome浏览器不会在控制台上显示这些代码

  4. 4

    我的 s3 存储桶中的图像不会在浏览器中打开,它们只会下载

  5. 5

    Firefox不会在浏览器中显示日志

  6. 6

    Firefox不会在浏览器中显示日志

  7. 7

    离子服务不会在浏览器中呈现

  8. 8

    在REACT中通过API构建无限加载时,即使我向下滚动很多,页面号也不会在控制台中更新

  9. 9

    Function.prototype不会在控制台中显示所有内置属性和方法

  10. 10

    在Rails控制台中写入文件不会在退出时保存

  11. 11

    UndoManager的canUndo属性不会在SwiftUI中更新

  12. 12

    为什么表达式不会在角度控制器中动态更新?

  13. 13

    C#SelectVoice不会在Windows应用程序中更改,但会在控制台中更改

  14. 14

    一对多字段不会在JPA中更新

  15. 15

    Tkinter不会在新目录中将映像文件更新为一个

  16. 16

    Oracle触发器不会在另一个表中插入值

  17. 17

    Rails全局变量不会在更新时更新

  18. 18

    单击Thunderbird中的http(s)链接不会在浏览器中打开该链接

  19. 19

    不会在IE浏览器的集合中删除Backbone js模型

  20. 20

    CSS Active Class不会在浏览器中得到尊重吗?

  21. 21

    我的Chromium浏览器对象不会在Winform中显示

  22. 22

    此代码不会在浏览器中显示tom

  23. 23

    更改后刷新页面不会在Web浏览器中呈现

  24. 24

    正确安装 Xampp 后,它不会在 Kali Linux 的浏览器中打开

  25. 25

    Cookie 不会在所有浏览器中过期/删除

  26. 26

    如何生成不会在浏览器中触发警告的自签名证书?

  27. 27

    Wordpress 主题中的 Bootstrap 不会在浏览器中呈现

  28. 28

    Fullpage.js 不会在任何浏览器(chrome、firefox、edge)中滚动

  29. 29

    Dart-运行项目不会加载dart文件,也不会在控制台中打印

热门标签

归档