我正在使用Ruby on Rails(5.1.4)上的应用程序 - 我最近为用户创建了具有2个参数的脚手架 - 用户名和名称。一段时间后,我更改了用户名索引。现在创建新用户时出现问题。显然 - 在每个模型,控制器等中我知道有用户名 - 我为Index更改了它。问题是 - 我可以这样做吗?或者我应该用正确的参数创建新的脚手架?如果是这样 - 我该怎么做?我不想失去控制器和视图的工作。
所以这就是我创建脚手架的方式:
rails generate scaffold Student index:string name:string
每次我尝试创建新用户时都会遇到错误:
我所做的改变:
应用程序/模型/ 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
应用程序/视图/用户/ _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 %>
应用程序/视图/用户/ _user.json.builder
json.extract! user, :id, :index, :name, :created_at, :updated_at
json.url user_url(user, format: :json)
应用程序/视图/用户/ 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 %>
应用程序/视图/用户/ 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 %>
应用程序/控制器/ 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
分贝/ 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
分贝/迁移/ 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
答案 0 :(得分:0)
您可以创建迁移以重命名模型中的属性,例如:
$ 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
来持续更改。
之后错误将持续存在,因为在其他文件中仍有一些索引引用:
validates :username ...