更改了数据库中的列名,现在显示不显示信息

问题描述 投票:0回答:1

我正在物流公司创建一个Web应用程序。我正确地创建了位置表。但我在其中一列中使用了“type”。我意识到“type”是Rails中的保留字,所以我在Locations表中进行了一次迁移,将“type”更改为“type_of_location”。

Locations表创建为脚手架。

所以在应用程序中,我点击“新位置”并输入相关详细信息。该应用程序说它成功保存,但缺少值。

我检查了数据库,除了created_on和updated_at之外,值为null。

我在这里看过各种文章,我也重新加载了数据库。

_form.html.erb

<%= form_for(@location) do |f| %>
  <% if @location.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2>

      <ul>
      <% @location.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :type_of_location %><br><small>(Select "Warehouse", "Office", "Server building" or "Other")</small>
    <%= f.text_field :type_of_location, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :address %><br>
    <%= f.text_field :address, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :address2 %><br>
    <%= f.text_field :address2, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :city %><br>
    <%= f.text_field :city, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :postal_code %><br>
    <%= f.text_field :postal_code, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :country %><br>
    <%= f.text_field :country, :class => 'form-control' %>
  </div>
  <div class="field">
    <%= f.label :reception_phone %><br>
    <%= f.text_field :reception_phone, :class => 'form-control' %>
  </div>
  <div class="actions">
    <%= f.submit 'Submit', :class => 'btn btn-success' %>
  </div>
<% end %>

routes.rb

...
resources :locations 

  resources :locations do
    resources :departments
    resources :employees
  end
...

show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Type of location:</strong>
  <%= @location.type_of_location %>
</p>

<p>
  <strong>Address:</strong>
  <%= @location.address %>
</p>

<p>
  <strong>Address2:</strong>
  <%= @location.address2 %>
</p>

<p>
  <strong>City:</strong>
  <%= @location.city %>
</p>

<p>
  <strong>Postal code:</strong>
  <%= @location.postal_code %>
</p>

<p>
  <strong>Country:</strong>
  <%= @location.country %>
</p>

<p>
  <strong>Reception phone:</strong>
  <%= @location.reception_phone %>
</p>

<%= link_to 'Edit', edit_location_path(@location) %> |
<%= link_to 'Back', locations_path %>

控制器/型号中的location.rb

class Location < ActiveRecord::Base

    has_many :departments
    has_many :employees

end

locations_controller.rb

class LocationsController < ApplicationController
  before_action :set_location, only: [:show, :edit, :update, :destroy]

  # GET /locations
  # GET /locations.json
  def index
    @locations = Location.all
  end

  # GET /locations/1
  # GET /locations/1.json
  def show
    @location = Location.find(params[:id])
  end

  # GET /locations/new
  def new
    @location = Location.new
  end

  # GET /locations/1/edit
  def edit
  end

  # POST /locations
  # POST /locations.json
  def create
    @location = Location.create(location_params)

    respond_to do |format|
      if @location.save
        format.html { redirect_to @location, notice: 'Location was successfully created.' }
        format.json { render :show, status: :created, location: @location }
      else
        format.html { render :new }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /locations/1
  # PATCH/PUT /locations/1.json
  def update
    respond_to do |format|
      if @location.update(location_params)
        format.html { redirect_to @location, notice: 'Location was successfully updated.' }
        format.json { render :show, status: :ok, location: @location }
      else
        format.html { render :edit }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /locations/1
  # DELETE /locations/1.json
  def destroy
    @location.destroy
    respond_to do |format|
      format.html { redirect_to locations_url, notice: 'Location was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_location
      @location = Location.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def location_params
      params.require(:location).permit(:type_of_location, :address, :address2, :city, :postal_code, :country, :reception_phone)
    end
end

schema.rb

...
  create_table "locations", force: :cascade do |t|
    t.string   "type_of_location"
    t.string   "address"
    t.string   "address2"
    t.string   "city"
    t.string   "postal_code"
    t.string   "country"
    t.string   "reception_phone"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end
...

[date]_create_locations.rb

class CreateLocations < ActiveRecord::Migration
  def change
    create_table :locations do |t|
      t.string :type
      t.string :address
      t.string :address2
      t.string :city
      t.string :postal_code
      t.string :country
      t.string :reception_phone

      t.timestamps null: false
    end
  end
end

插入后输出:

Started POST "/locations" for 193.1.209.157 at 2019-03-28 12:59:33 +0000
Cannot render console from 193.1.209.157! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by LocationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"L/sFGam4dDoKQ5yQ1M95sUpCg6HW6MnQ3IzUFslfBUe4Udt11z14qXg/A/PCw1EaZq0vU/LN+Hb9eXO92w8lwQ==", "location"=>{"type_of_location"=>"Warehouse", "address"=>"57 O'Connell Street, Dublin", "address2"=>"Dublin", "city"=>"Dublin", "postal_code"=>"D01AB12", "country"=>"Ireland", "reception_phone"=>"+14516631"}, "commit"=>"Submit"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "locations" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2019-03-28 12:59:33.740960"], ["updated_at", "2019-03-28 12:59:33.740960"]]
   (13.2ms)  commit transaction
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to https://a2z-ie-app-keithfeeneynci.c9users.io/locations/2
Completed 302 Found in 21ms (ActiveRecord: 14.2ms)


Started GET "/locations/2" for 193.1.209.157 at 2019-03-28 12:59:34 +0000
Cannot render console from 193.1.209.157! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by LocationsController#show as HTML
  Parameters: {"id"=>"2"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Location Load (0.1ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."id" = ? LIMIT 1  [["id", 2]]
  Rendered locations/show.html.erb within layouts/application (1.2ms)
  Rendered devise/menu/_registration_items.html.erb (0.2ms)
  Rendered devise/menu/_login_items.html.erb (0.2ms)
Completed 200 OK in 34ms (Views: 31.7ms | ActiveRecord: 0.3ms)
ruby-on-rails sqlite
1个回答
0
投票

我无法解决这个问题,所以我再次启动了我的项目。我确定“type”是“location_type”,它有效,没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.