Ruby on rails,改变最近创建的脚手架

Posted

技术标签:

【中文标题】Ruby on rails,改变最近创建的脚手架【英文标题】:Ruby on rails, changing recently created scaffold 【发布时间】:2018-06-23 07:24:03 【问题描述】:

我正在使用 Ruby on Rails (5.1.4) 开发应用程序——我最近为用户创建了具有 2 个参数的脚手架——用户名和名称。一段时间后,我更改了索引的用户名。现在创建新用户有问题。显然-在每个模型、控制器等中。我知道有用户名-我将其更改为索引。问题是——我可以这样做吗?或者我应该用正确的参数创建新的脚手架?如果是这样 - 我该怎么做?我不想失去我在控制器和视图方面的工作。

这就是我创建脚手架的方式:

rails generate scaffold Student index:string name:string

这是我每次尝试创建新用户时都会遇到的错误:

Error

以及我所做的更改:

app/models/user.rb

class User < ApplicationRecord
    has_and_belongs_to_many :movies, :join_table => :users_movies
    has_many :topics
    has_many :posts

    has_secure_password

    validates :name, presence: true, uniqueness: true, length:  in: 3..50 
    validates :index, presence: true, length:  is: 6 , uniqueness: true
    validates :password, presence: true, length:  minimum: 6 

    def follows?(movie)
        self.movies.include?(movie)
    end
end

app/views/users/_form.html.erb

<%= form_with(model: user, local: true) do |form| %>
  <% if user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

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

  <div class="field">
    <%= form.label :index %>
    <%= form.text_field :index, id: :user_index %>
  </div>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :user_name %>
  </div>

  <div class="field">
    <%= form.label :password %>
    <%= form.password_field :password, id: :users_password %>
  </div>

  <div class="field">
    <%= form.label :password_confirmation %>
    <%= form.password_field :password_confirmation, id: :user_password_confirmation %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

app/views/users/_user.json.builder

json.extract! user, :id, :index, :name, :created_at, :updated_at
json.url user_url(user, format: :json)

app/views/users/index.html.erb

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

<h1>Users</h1>

<table>
  <thead>
    <tr>
      <th>Index</th>
      <th>Name</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.index %></td>
        <td><%= user.name %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data:  confirm: 'Are you sure?'  %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>

app/views/users/show.html.erb

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

<p>
  <strong>Index:</strong>
  <%= @user.index %>
</p>

<p>
  <strong>Name:</strong>
  <%= @user.name %>
</p>

<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>

app/controllers/user_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

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

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

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html  redirect_to users_url, notice: 'User was successfully destroyed.' 
      format.json  head :no_content 
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:index, :name, :password, :password_confirmation)
    end
end

db/schema.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180113170026) do

  create_table "movies", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string "body"
    t.integer "user_id"
    t.integer "topic_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["topic_id"], name: "index_posts_on_topic_id"
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "topics", force: :cascade do |t|
    t.string "title"
    t.integer "user_id"
    t.integer "movie_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["movie_id"], name: "index_topics_on_movie_id"
    t.index ["user_id"], name: "index_topics_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "index"
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_digest"
    t.index [nil], name: "index_users_on_index", unique: true
  end

  create_table "users_movies", id: false, force: :cascade do |t|
    t.integer "user_id"
    t.integer "movie_id"
    t.index ["movie_id"], name: "index_users_movies_on_movie_id"
    t.index ["user_id"], name: "index_users_movies_on_user_id"
  end

end

db/migrate/20171125194647_create_users.rb

class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :index
      t.string :name

      t.timestamps
    end
  end
end

【问题讨论】:

您是否尝试重建您的数据库? 【参考方案1】:

您可以创建迁移以重命名模型中的属性,例如:

$ rails g migration rename_index_to_username

在迁移文件中,指定要更新的模型作为 rename_column 的第一个参数,然后是旧属性名称和新属性名称:

class RenameIndexToUsername < ActiveRecord::Migration[5.1]
  def change
    rename_column :users, :index, :username
  end
end

然后运行 ​​rails db:migrate 以保留更改。

之后错误会一直存在,因为其他文件中还有一些对index的引用:

首先编辑 Rails 创建的 user_params,将 index 替换为 username。 在显示和索引页面中,将索引属性的任何引用替换为用户名。 将表单中指向索引的 text_field 助手替换为用户名。 最后将模型中的验证也替换为validates :username ...

【讨论】:

以上是关于Ruby on rails,改变最近创建的脚手架的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails - rails g 脚手架外键

ruby on rails 路线

Ruby On Rails在脚手架之后添加新页面

在创建控制器和模型之后(仅)创建 Ruby on Rails 视图

如何在 Ruby on Rails 中链接个人资料页面

如何在 ruby​​ on rails 中更改路线?