gpt4 book ai didi

ruby-on-rails - 是否有任何功能可以在 Ruby on Rails 中创建模型/表列名称的自动值?

转载 作者:行者123 更新时间:2023-11-29 13:39:48 24 4
gpt4 key购买 nike

我正在使用 Postgresql 设置新的简单 ruby​​ on rails 博客 api,它必须包含 2 个部分。第一部分是为 CRUD 创建简单的博客。它已经准备好了。第二部分是三角形检查器......我添加了简单的三角形 MVC 文件,它有 4 个模型属性 > name,edge1,edge2,edge3......现在我的程序必须分配三角形名称 Scalene/等边/自动等腰。例如,用户输入 edge1=edge2=edge3=4 并且名称必须自动为“Equilatera”...并且我必须将边长添加到 URL 地址...例如:http://localhost:3000/triangles/2/a=3,b=3,c=3,'等距'

有没有简单的解决办法或者方法或者功能或者推荐?所有文件都在这里: https://github.com/SuperSayiyajin/My_Ruby_Codes

谢谢...

  • 我试图修改 triangle.rb 类文件,但是当我创建 initialize(edge1,edge2,edge3) 方法时,来自 triangle-controller 的新方法给我的参数错误给定 1 expected 3...

  • 我尝试向 Controller 添加一些 if 语句 block 并查看 rb 文件,但我失败了。它不起作用。

  • 我尝试为数据库迁移创建 Postgresql 触发器,但它给了我BEGIN 仅在顶层错误时允许。

  • 我的三角模型

class Triangle < ApplicationRecord
validates :edge1, presence: true
validates :edge2, presence: true
validates :edge3, presence: true

def triangle(a, b, c)
t = Triangle.new(a, b, c)
return t.type
end

def initialize(a, b, c)
@edge1 = a
@edge2 = b
@edge3 = c
@sides = [a,b,c].sort
ValidLength
end

def type
case @sides.uniq.size
when 1 then :equilateral
when 2 then :isosceles
else :scalene
end
end

private
def ValidLength
if @sides.any? { |x| x <= 0 }
raise TriangleError, "Sides must be greater than 0"
end

if @sides[0] + @sides[1] <= @sides[2]
raise TriangleError, "Not valid triangle lengths"
end
end
end

-我的三角形模型错误 https://imge.to/i/51ydR

  • 我的 Postgresql 触发器代码。我在 db/migration 中创建,当我运行 rake db:migrate:up VERSION=my_migration_name.rb >> 它给了我 BEGIN is permitted only at toplevel 错误。

def change
CREATE TRIGGER triangles_name_insert AFTER INSERT ON triangles
FOR EACH ROW
BEGIN
if edge1 + edge2 < edge3
UPDATE triangles SET name = 'Invalid Edges';
else
if edge1=edge2 and edge1=edge3
UPDATE triangles SET name = 'Equilateral' ;
elsif edge1=edge or edge1=edge3 or edge2=edge3
UPDATE triangles SET name = 'Isosceles' ;
else
UPDATE triangles SET name = 'Scalane' ;
end
end
END;

CREATE TRIGGER account_users_after_update_on_name_row_tr AFTER UPDATE ON account_users
FOR EACH ROW
BEGIN
if edge1 + edge2 < edge3
UPDATE triangles SET name = 'Invalid Edges';
else
if edge1=edge2 and edge1=edge3
UPDATE triangles SET name = 'Equilateral' ;
elsif edge1=edge or edge1=edge3 or edge2=edge3
UPDATE triangles SET name = 'Isosceles' ;
else
UPDATE triangles SET name = 'Scalane' ;
end
end
END;
end

-我的 Postgresql 数据库:迁移错误 https://imge.to/i/51MXk

-我的三角 Controller

class TrianglesController < ApplicationController
before_action :set_triangle, only: [:show, :edit, :update, :destroy]

# GET /triangles
# GET /triangles.json
def index
@triangles = Triangle.all
end

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

# GET /triangles/new
def new
@triangle = Triangle.new
end

# GET /triangles/1/edit
def edit
end

# POST /triangles
# POST /triangles.json
def create
@triangle = Triangle.new(triangle_params)
note=" "
respond_to do |format|
if @triangle.save
if [triangle_params].uniq.size == 1
note='Triangle was successfully created.And it is equilateral'
elsif [triangle_params].uniq.size == 2
note='Triangle was successfully created.And it is isosceles'
else
note='Triangle was successfully created.And it is scalene'
end
format.html { redirect_to @triangle, notice: note}
#format.json { render :show, status: :created, location: @triangle }
else
format.html { render :new }
#format.json { render json: @triangle.errors, status: :unprocessable_entity }
end
end
end

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

# DELETE /triangles/1
# DELETE /triangles/1.json
def destroy
@triangle.destroy
respond_to do |format|
format.html { redirect_to triangles_url, notice: 'Triangle was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_triangle
#case [:edge1,:edge2,:edge3].uniq.size
#when 1 then :equilateral
#when 2 then :isosceles
#else :scalene
@triangle = Triangle.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def triangle_params
params.require(:triangle).permit(:edge1, :edge2, :edge3)
end
end
  • 我的 index.hmtl.erb 文件

三角形

姓名 边缘 1 边缘2 边缘3 特性
```

最佳答案

我同意 Sergio 的观点,并且在这种情况下也更喜欢使用 before_save 触发器。应该做的工作不多,所以用 ruby 来做就可以了,而不是将这个应用程序逻辑转移到数据库。

无论如何,如果你想坚持使用 Postgres 触发器,那么你可以使用 reversible migration syntaxold up/down one :

class AddTriangleTriggersMigration < ActiveRecord::Migration[5.0]
def change
reversible do |dir|
# Specify what to do on 'rails db:migrate' (create triggers)
dir.up do
execute <<-SQL
CREATE TRIGGER triangles_name_insert AFTER INSERT ON triangles
FOR EACH ROW
BEGIN
if edge1 + edge2 < edge3
UPDATE triangles SET name = 'Invalid Edges';
else
if edge1=edge2 and edge1=edge3
UPDATE triangles SET name = 'Equilateral' ;
elsif edge1=edge or edge1=edge3 or edge2=edge3
UPDATE triangles SET name = 'Isosceles' ;
else
UPDATE triangles SET name = 'Scalane' ;
end
end
END;

CREATE TRIGGER account_users_after_update_on_name_row_tr AFTER UPDATE ON account_users
FOR EACH ROW
BEGIN
if edge1 + edge2 < edge3
UPDATE triangles SET name = 'Invalid Edges';
else
if edge1=edge2 and edge1=edge3
UPDATE triangles SET name = 'Equilateral' ;
elsif edge1=edge or edge1=edge3 or edge2=edge3
UPDATE triangles SET name = 'Isosceles' ;
else
UPDATE triangles SET name = 'Scalane' ;
end
end
END;
SQL
end

# Specify what to do on 'rails db:rollback' (drop triggers)
dir.down do
execute <<-SQL
DROP TRIGGER triangles_name_insert;
DROP TRIGGER account_users_after_update_on_name_row_tr;
SQL
end
end
end
end

我没有检查你的 SQL 触发器的正确​​性,但如果你想用普通 SQL 编写复杂的迁移,那么你应该以这种方式进行,以便它们是可逆的。

关于ruby-on-rails - 是否有任何功能可以在 Ruby on Rails 中创建模型/表列名称的自动值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57104154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com