2013-11-15 3 views
2
내가 사무실에서 사람들을 대표하는 간단한 레일 응용 프로그램을 만들고있어

을에, 콘솔에 업데이트됩니다 - 각 사람의 이름, 이메일, 책상 번호 등레일 : 하나의 속성 만하지 브라우저

내 문제가 분명히 간과 한 간단한 내용입니다. 새로운 사람을 만들어 책상 번호를 주거나 기존 사람을 업데이트하여 브라우저에서 응용 프로그램을 사용하여 책상 번호를 변경하면 변경 사항이 고려되지 않습니다. 콘솔의 데스크 번호 만 업데이트합니다.

필자는 '사람'을 만들 때가 아니라 '책상'속성을 별도로 추가했기 때문에 내가 놓친 부분이 있다고 생각합니다.

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] 

모든 요소가 여기에 잘 떨어져 '책상'에서 업데이트 할 수 있습니다.

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 : 레일 콘솔

  • , 난 수행

    p = Person.find_by (ID : 2)

    P. update_attribute : 데스크, 10

    012 내가 이동 3,516,
  • /인/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' 
+1

컨트롤러 코드 – Aguardientico

+0

뿐만 아니라 양식 코드를 추가하십시오. 'show' 액션의 코드는 여기서는 전혀 관련이 없습니다. –

+0

컨트롤러 및 양식 코드 추가 – aurumpotestasest

답변

2

당신은 당신의 허용 매개 변수에 desk를 두지 않았다

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

감사합니다. 추가 속성을 추가하면 person_params를 업데이트해야한다는 것을 알지 못했습니다! – aurumpotestasest

+0

@Marek Lipka 좋은 장소! 나는 당신이 레코드를 생성/업데이트하려고 시도하고 허용 된 매개 변수에서 속성 중 하나를 지정하지 않은 경우 Rails에서 오류가 발생했다고 생각했습니다. 경고로 서버 로그에 저장합니까? –